【问题标题】:Start and stop animation with CABasicAnimation iphone使用 CABasicAnimation iphone 开始和停止动画
【发布时间】:2011-04-30 04:55:32
【问题描述】:

我想对图像使用动画,从单击按钮开始并在 5 秒后停止。我使用此代码。

-(IBAction) btnClicked
{   
    CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 6;
    fullRotation.repeatCount = 1e100f;
    fullRotation.delegate = self;
    [imgView.layer addAnimation:fullRotation forKey:@"360"];
    [imgView.layer setSpeed:3.0];
}

使用此代码动画已启动,但我不知道如何在 5 秒后停止此动画。

【问题讨论】:

标签: iphone animation cabasicanimation


【解决方案1】:

甚至不要处理CoreAnimation。我认为这可以通过UIView 动画来做到这一点,这更容易编程。查看UIView 文档并尝试块动画(假设iOS 4.0。如果不是,请使用旧样式)。

查看here 以获取有关UIView 动画的文档。设置transform 属性以使用CGAffineTransformMakeRotation 进行旋转。

这是一个基于UIView 的动画旋转:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *viewToAnimate = [[[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)] autorelease];
    viewToAnimate.backgroundColor = [UIColor blueColor];
    [self.view addSubview:viewToAnimate];
    [UIView animateWithDuration:5 animations:^{
        viewToAnimate.transform=CGAffineTransformMakeRotation(M_PI);
    }];
}

【讨论】:

  • 谢谢 Davidneiss。但是我想要这个顺时针和逆时针的动画,UIView 动画可以吗?
猜你喜欢
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多