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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...