【问题标题】:Objective C - Custom UIView Class Init With AnimationObjective C - 带有动画的自定义 UIView 类初始化
【发布时间】:2017-06-23 17:08:21
【问题描述】:

我有一个简单的自定义 UIView 类和一个关联的 xib 文件,其中包含一个 ImageView,我想在视图初始化时开始旋转。

下面是我的 UIView 代码。使用断点我可以确认代码被调用:

#import "PinWithTimeView.h"
#import "QuartzCore/QuartzCore.h"


@implementation PinWithTimeView

- (id)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];

    if (self) {

        CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        fullRotation.fromValue = [NSNumber numberWithFloat:0];
        fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
        fullRotation.duration = 6;
        fullRotation.repeatCount = 10;
        [self.rotateCircle.layer addAnimation:fullRotation forKey:@"360"];

    }

    return self;
}


@end

但是,当我这样初始化时,ImageView 永远不会旋转。谁能告诉我为什么?

PinWithTimeView *pinMarker = [[[NSBundle mainBundle] loadNibNamed:@"PinWithTimeView" owner:self options:nil] firstObject];

【问题讨论】:

    标签: ios objective-c uiview


    【解决方案1】:

    我认为您不能为视图/图层设置动画,除非它是视图层次结构的一部分。在 init/initWithCoder 中还没有添加。

    您可以尝试实现 UIView 方法 didMoveToSuperview 方法。

    【讨论】:

      【解决方案2】:

      我通过将动画块移动到layoutSubviews 方法中来实现这一点。感谢 Duncan 为我指明了正确的方向,该视图还不是 initWithCoder 中的层次结构的一部分@

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多