xcode – CABasicAnimation无法使用UIImageView

前端之家收集整理的这篇文章主要介绍了xcode – CABasicAnimation无法使用UIImageView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个以编程方式调用的UI ImageView,我试图让它旋转但它不起作用.图像将被放置在动态UITableView中(我无法将其更改为静态).我的表视图中的图像看起来不错,但它不会旋转.
- (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中创建图像视图实例,但是等到viewDidAppear:被调用以实际启动动画.
@property (strong,nonatomic) UIImageView *imageView;

然后

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,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"];
}

猜你在找的iOS相关文章