带有旋转动画问题的自定义旋转器类



i编程了自己的视图,其中包含一个应该旋转的imageView。这是我的旋转动画:

- (void)startPropeller
{
    //_movablePropeller = [[UIImageView alloc] initWithFrame:self.frame];
    //_movablePropeller.image = [UIImage imageNamed:@"MovablePropeller"];
    //[self addSubview:self.movablePropeller];
    self.hidden = NO;
    CABasicAnimation *rotation;
    rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0.0f];
    rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
    rotation.cumulative = true;
    rotation.duration = 1.2f; // Speed
    rotation.repeatCount = INFINITY; // Repeat forever. Can be a finite number.
    [self.movablePropeller.layer removeAllAnimations];
    [self.movablePropeller.layer addAnimation:rotation forKey:@"Spin"];
}

这就是我开始的方式:

self.loadingPropeller = [[FMLoadingPropeller alloc] initWithFrame:self.view.frame andStyle:LoadingPropellerStyleNoBackground];
self.loadingPropeller.center=self.view.center;
[self.view addSubview:self.loadingPropeller];
[self.loadingPropeller startPropeller];

问题是:没有任何进一步的代码。螺旋桨不旋转。因此,我能够通过将此代码添加到我的类中,以将其添加到旋转螺旋桨旋转器中:

-(void)viewDidAppear:(BOOL)animated
{
    if(!self.loadingPropeller.hidden){
        [self.loadingPropeller startPropeller];
    }
}

,但我不太喜欢太多。难道不可能在螺旋桨类中添加一些代码来自动解决此问题,而无需在ViewDidAppear方法中的每个类中添加代码?

不起作用的代码做两个基本的事情:将旋转器添加到视图层次结构并将其定位。我的猜测是,故障是由于在布局发生之前将其定位。尝试以下操作:

// in viewDidLoad of the containing vc...
self.loadingPropeller = [[FMLoadingPropeller alloc] initWithFrame:CGRectZero andStyle:LoadingPropellerStyleNoBackground];
[self.view addSubview:self.loadingPropeller];
// within or after viewDidLayoutSubviews...
// (make sure to call super for any of these hooks)
self.loadingPropeller.frame = self.view.bounds;
self.loadingPropeller.center = self.view.center;
// within or after viewDidAppear (as you have it)...
[self.loadingPropeller startPropeller];

最新更新