Ios Objective C-带有动画的自定义UIView类初始化

Ios Objective C-带有动画的自定义UIView类初始化,ios,objective-c,uiview,Ios,Objective C,Uiview,我有一个简单的自定义UIView类和一个关联的xib文件,其中包含一个ImageView,我希望在初始化视图时开始旋转它 下面是我的UIView代码。使用断点,我可以确认代码是否被调用: #import "PinWithTimeView.h" #import "QuartzCore/QuartzCore.h" @implementation PinWithTimeView - (id)initWithCoder:(NSCoder *)aDecoder { self = [supe

我有一个简单的自定义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];

我认为,除非视图/层是视图层次结构的一部分,否则无法为其设置动画。在init/initWithCoder中,它还没有被添加


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

我通过将动画块移动到
layoutSubviews
方法来实现这一点。感谢邓肯为我指出了正确的方向,在
initWithCoder