objective-c - 自定义 CALayer 分配后 UIButton 不检测触摸

标签 objective-c ios uibutton calayer

除非注释掉下面的委托(delegate)分配,否则我的 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
}

最佳答案

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

关于objective-c - 自定义 CALayer 分配后 UIButton 不检测触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12874752/

相关文章:

ios - UIButton 左上角和左下角半径显示不正确

jquery - 使用自己的图像更改 jQuery UI 按钮的图标

ios - Objective-C:如何从 POST 方法返回响应?

iOS 系统在打开 App Store URL 之前挂起

iphone - Xcode 调试器不停止 i ipod 与 IOS 5 Beta

iOS - 尝试使用 Fabric 存档时出现构建错误 (Twitter)

ios - IBoutlet 如何保持初始值变弱?意味着其他一些强大的物体指向?

c++ - 使用 OpenCV 触摸和选择对象

ios - 在 UITableViewController 中启用滚动

ios - 我的自定义按钮在 iCarousel iOS 中没有获得点击?