Objective C:螺旋桨形式的自定义旋转动画器 class

Objective C: Custom spinning animator class in form of a propeller

我基本上希望在加载内容时在屏幕上显示我自己的动画师。我想要一个固定齿轮环图像顶部的螺旋桨(应该旋转)。这个想法是用一个特定的框架来实例化一个对象,该框架定义了装载螺旋桨的大小。然后像使用普通微调器一样开始和停止旋转。

但在我看来,没有显示任何内容。我对自己的视图编程以及如何根据如何将它们放置在屏幕中央并使其大小正确来布局它们是新手。所以我想我的错误在于正确设置框架和中心,但我不知道。也许你能帮上忙:

这是我的装载螺旋桨class:

@implementation FMLoadingPropeller

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;

        _movablePropeller = [[UIImageView alloc] initWithFrame:self.frame];
        _movablePropeller.image = [UIImage imageNamed:@"PropWhite"];
        _movablePropeller.frame = self.frame;

        _fixedPropellerPart = [[UIImageView alloc] initWithFrame:self.frame];
        _fixedPropellerPart.image = [UIImage imageNamed:@"GearringBlue"];
        _fixedPropellerPart.frame = self.frame;

        [self addSubview:_fixedPropellerPart];
        [self addSubview:_movablePropeller];
    }
    return self;
}

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

- (void)stopPropeller
{
    self.hidden = YES;

    [self.movablePropeller.layer removeAllAnimations];
}

@end

这就是它的实例化和启动方式:

self.loadingPropeller = [[FMLoadingPropeller alloc] initWithFrame:CGRectMake(self.view.center.x, self.view.center.y, 200, 200)];
self.loadingPropeller.center=self.view.center;
[self.view addSubview:self.loadingPropeller];
[self.loadingPropeller startPropeller];

但是屏幕上没有任何显示...

编辑: 现在它出现在屏幕上但在最右下角

您在创建两个图像视图时传递了错误的帧。请记住,视图的框架需要与其父视图相关。

变化:

_movablePropeller = [[UIImageView alloc] initWithFrame:self.frame];

至:

_movablePropeller = [[UIImageView alloc] initWithFrame:self.bounds];

同时删除以下行:

_movablePropeller.frame = self.frame;

框架已在 initWithFrame 调用中设置。

同时确认图像确实正在加载。您可能忘记将图像添加到应用程序包中。

您需要右居中才能查看

show view controller 和 load all subview 后会出现右中

你会帮助方法viewDidLayoutSubviews

代码:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.loadingPropeller.center = self.view.center; // changed you x,y
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.loadingPropeller = [[FMLoadingPropeller alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    [self.view addSubview:self.loadingPropeller];
    [self.loadingPropeller startPropeller];
}