【问题标题】:iphone : animating a CALayer sublayeriphone : 动画 CALayer 子层
【发布时间】:2010-11-16 21:36:01
【问题描述】:

我正在尝试使用以下代码为 UIView 图层的子图层设置动画:

CALayer *sublayer = [[CALayer alloc] init];
[sublayer setFrame:myRect]; // myRect is a portion of the view's frame rectangle
[self.layer addSublayer:sublayer];

后来在我的 drawRect: 方法中:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.duration = 1.0f;
anim.fromValue = [NSNumber numberWithFloat:1];
anim.toValue = [NSNumber numberWithFloat:.5];
anim.delegate = self;
[sublayer addAnimation:anim forKey:@"animateOpacity"];

我不明白为什么我的子层没有动画,因为代码 [self.layer addAnimation:anim forKey:@"animateOpacity"]; 确实为视图的主层设置了动画。

谢谢。

【问题讨论】:

    标签: iphone objective-c core-animation calayer


    【解决方案1】:

    不要在drawRect: 方法期间添加CAAnimations。 CoreAnimation 在需要重绘视图内容时调用它(可能在任何时候)。您应该在视图添加到其父视图后添加动画。

    【讨论】:

      【解决方案2】:

      您不能为 frame 属性设置动画。而是为边界或位置设置动画。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多