【问题标题】:UIButton Doesn't Detect Touches After Custom CALayer Assignment自定义 CALayer 分配后 UIButton 不检测触摸
【发布时间】:2012-10-13 15:55:00
【问题描述】:

除非下面的委托分配被注释掉,否则我的 UIButton 的目标操作不会被调用,但是我会丢失我的自定义绘图。我错过了什么?

- (void)createLayerForPaletteAtIndex:(int)paletteIndex onButton:(UIButton *)button
{
    self.colorPaletteLayer            = button.layer;
    self.colorPaletteLayer.delegate   = self; // Must implement drawLayer:inContext
    NSString *stringForIndex          = [NSString stringWithFormat:@"%d",paletteIndex];
    self.colorPaletteLayer.name       = stringForIndex;
    button.clearsContextBeforeDrawing = YES;
    button.layer.opaque               = YES;
    [self.colorPaletteLayer setNeedsDisplay]; // Calls drawLayer:inContext
}

【问题讨论】:

    标签: objective-c ios uibutton calayer


    【解决方案1】:

    您正在劫持按钮的图层。你不应该那样做。 UIKit 不希望你这样做。您应该创建一个子层并将其添加到按钮的层。

    【讨论】:

    • 我最初是作为子层添加的,但是直接分配层时性能会更好。 (这是 UITableView 中的众多按钮之一。)我猜这不是一个可行的优化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多