Xcode 不使用UIImageView的Cabasicanation
我有一个通过编程调用的UIImageView,我试图让它旋转,但它不工作。图像将放置在动态UITableView中(我无法将其更改为静态)。图像在我的表视图中看起来很好,但它只是不旋转Xcode 不使用UIImageView的Cabasicanation,xcode,uitableview,uiimageview,cabasicanimation,Xcode,Uitableview,Uiimageview,Cabasicanimation,我有一个通过编程调用的UIImageView,我试图让它旋转,但它不工作。图像将放置在动态UITableView中(我无法将其更改为静态)。图像在我的表视图中看起来很好,但它只是不旋转 - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)]; imageView.i
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
imageView.image = [UIImage imageNamed:@"settings1.png"];
[self.view addSubview:imageView];
CABasicAnimation *spin;
spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[imageView.layer addAnimation:spin forKey:@"360"];
这可能是因为您试图在viewDidLoad中启动动画。我建议以这样一种方式重新组织代码:在viewDidLoad中创建图像视图实例,然后等待调用ViewDidDisplay:以实际启动动画
@property (strong, nonatomic) UIImageView *imageView;
然后
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 122, 38, 38)];
self.imageView.image = [UIImage imageNamed:@"settings1.png"];
[self.view addSubview:self.imageView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spin.fromValue = [NSNumber numberWithFloat:0];
spin.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
spin.duration = 4;
spin.repeatCount = 10*1000;
[self.imageView.layer addAnimation:spin forKey:@"360"];
}
谢谢这么简单的解决方案,仅供参考,为什么这个方法有效?是因为它一直等到viewDidLoad?基本上,在视图实际出现在屏幕上之前,您不应该尝试启动动画。在调用viewDidLoad时,视图控制器的视图已加载,但实际上尚未添加到视图层次结构中,因此有时视图或其子视图上的动画和类似内容可能会被抛出,因为它们在生命周期中发生得太早。