具有旋转动画问题的自定义Spinner类

时间:2019-05-20 12:18:52

标签: objective-c animation spinner

我编写了自己的视图,其中包含应该旋转的imageview。这是我的旋转动画:

grid = st_sf(n = lengths(tab), geometry = st_cast(grid, "MULTIPOLYGON"))

mapview(grid, zcol = "n")

这是我的启动方式:

- (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];

但是我不太喜欢。不必在Propeller类中添加一些代码来自动解决此问题,而不必在viewDidAppear方法的每个类中也添加代码吗?

1 个答案:

答案 0 :(得分:0)

无效的代码执行两项基本操作:将微调器添加到视图层次结构并将其定位。我的猜测是失败是由于在布局发生之前就将其定位。试试这个:

// 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];