Objective c 目标C:定制螺旋桨形式的旋转动画类
我基本上希望我自己的动画师在加载东西时显示在屏幕上。我想要一个螺旋桨(它应该旋转)在一个固定的齿圈图像的顶部。其思想是用一个定义加载推进器大小的特定帧实例化一个对象。然后像普通的旋转器一样开始和停止旋转 但在我看来,什么都没有表现出来。我对编程我自己的视图以及如何布局视图并不熟悉,因为我知道如何将视图放在屏幕中央,并使其大小正确。所以我猜我的错误是在设置正确的帧和中心,但我不知道。也许你能帮上忙: 这是我的装载螺旋桨类:Objective c 目标C:定制螺旋桨形式的旋转动画类,objective-c,animation,uiactivityindicatorview,Objective C,Animation,Uiactivityindicatorview,我基本上希望我自己的动画师在加载东西时显示在屏幕上。我想要一个螺旋桨(它应该旋转)在一个固定的齿圈图像的顶部。其思想是用一个定义加载推进器大小的特定帧实例化一个对象。然后像普通的旋转器一样开始和停止旋转 但在我看来,什么都没有表现出来。我对编程我自己的视图以及如何布局视图并不熟悉,因为我知道如何将视图放在屏幕中央,并使其大小正确。所以我猜我的错误是在设置正确的帧和中心,但我不知道。也许你能帮上忙: 这是我的装载螺旋桨类: @implementation FMLoadingPropeller /
@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.frame = self.frame;
帧已在initWithFrame
调用中设置
还要确认图像确实正在加载。您可能忘记将图像添加到应用程序包中。在创建两个图像视图时,您传递了错误的帧。请记住,视图的框架需要相对于其父视图 更改:
_movablePropeller = [[UIImageView alloc] initWithFrame:self.frame];
致:
另外,请删除该行:
_movablePropeller.frame = self.frame;
帧已在initWithFrame
调用中设置
还要确认图像确实正在加载。您可能忘记将图像添加到应用程序包中。您需要右中心查看 “显示视图控制器”和“加载所有子视图”之后将显示右中心 你会帮我的 代码:
您需要右视图中心 “显示视图控制器”和“加载所有子视图”之后将显示右中心 你会帮我的 代码:
非常感谢你的评论。好的,我已经改变了所有这些,图像也被加载了。但它们显示在我主视图右下角的屏幕外。在我的第一篇评论中,我将init调用从主视图更改为已编辑的部分,但没有任何帮助。感谢您的评论。好的,我已经改变了所有这些,图像也被加载了。但它们显示在我主视图右下角的屏幕外。在我的第一条评论中,我将init调用从主视图更改为编辑的部分,但没有任何帮助