【问题标题】:CALayer not rendering or drawingCALayer 不渲染或绘图
【发布时间】:2011-11-11 01:18:07
【问题描述】:

我在子类UITableViewCell 中有一个子类UIButton。该按钮是从笔尖加载的。

我有一张图片想用作按钮的图片。我想使用CALayer 来更好地控制动画。当用户点击按钮时,会出现动画。但是,我什至无法显示图像。

QuartzCore 已导入。

我的子类UIButton 的代码(总是从笔尖加载):

-(void)awakeFromNib {
    [super awakeFromNib];
    self.clipsToBounds = NO;
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.userInteractionEnabled = YES;
}

表格视图控制器中的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *cell = (BLCustomCellCenter*)[tableView dequeueReusableCellWithIdentifier:@"customCellID"];
    if (cell == nil) {
        // ... cell is initialized
    }

    // configure the image for the button
    //
    // Note:  there is an outlet to the UIButton called 'customButton' 

    CALayer *imgLayer = [CALayer layer];
    imgLayer.bounds = CGRectMake(0, 0, cell.customButton.bounds.size.width, cell.customButton.bounds.size.height);
    imgLayer.position = CGPointMake(cell.customButton.bounds.size.width/2, cell.customButton.bounds.size.height/2);
     UIImage *img = [UIImage imageNamed:@"someImage"];
    imgLayer.contents = (id)[img CGImage];
    [cell.customButton.layer addSublayer:imgLayer];

    // ... configure subviews of 'customButton'

    return cell;
}

任何帮助都非常感谢,一如既往。

【问题讨论】:

    标签: iphone ios cocoa-touch core-animation calayer


    【解决方案1】:

    经过数小时的调试,终于搞定了。事情(也许我忘了提)是自定义的UITableViewCell 是从笔尖加载的。我想查看的子视图也是从笔尖加载的。所以,它是一个笔尖中的一个笔尖。

    根据this wonderful article,在子视图的类中覆盖awakeAfterUsingCoder: 就可以了。加载父 nib 时,在仅加载占位符对象的子视图上调用超级的 initUsingCoder:

    这个“占位符”对象导致了我的问题,因为我正在操作占位符而不是我想要的实际对象。

    因此,您必须从 nib 加载和初始化子视图的对象,您可以通过覆盖 NSObjectawakeAfterUsingCoder: 来做到这一点。

    【讨论】:

      【解决方案2】:

      设置内容后尝试添加[imgLayer setNeedsDisplay];

      【讨论】:

      • 你验证过 img 不是 nil 吗?
      • 不是零。 NSLog 给了我反馈,例如“”。
      猜你喜欢
      • 2022-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多