目标C:采用螺旋桨形式的定制旋转动画制作器类

时间:2019-05-09 21:23:00

标签: objective-c animation uiactivityindicatorview

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

但是我认为没有任何显示。我对编程自己的视图以及如何布局它们(将其置于屏幕中央并使其具有正确的大小)感到陌生。所以我想我的错误在于设置正确的框架和中心,但我不知道。也许您可以提供帮助:

这是我的装载螺旋桨课程:

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

但是屏幕上什么也没有出现...

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

2 个答案:

答案 0 :(得分:2)

创建两个图像视图时传递的帧错误。请记住,视图的框架必须相对于其父框架。

更改:

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

收件人:

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

也删除该行:

_movablePropeller.frame = self.frame;

该帧已在initWithFrame调用中设置。

还请确认图像实际上已被加载。您有可能忘记将图像添加到应用包中。

答案 1 :(得分:0)

您需要右中心视图

右侧中心将显示在显示视图控制器之后并加载所有子视图

您将帮助方法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];
}