iOS UIView动画曲线

16
有没有什么像下面这样的“简短易懂”的方式来完成事情?曲线似乎仍然使用了。
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView animateWithDuration:0.5 animations:^{
    // ... do stuff here
}];
3个回答

31

您混合使用了两种不同类型的UIView动画。您应该使用以下任一方式之一:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                    // ... do stuff here
               } completion:NULL];

这是使用较新的基于块的UIView动画API生成的代码。而第一行则属于旧的UIView动画API,看起来像这样:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// ... do stuff here
[UIView commitAnimations];

两者都能实现同样的功能,你可以使用任意一种方式(不过苹果公司建议使用基于块的API,因为它更容易/更干净地在动画完成后执行回调)。


2

4
请注意,SO上不鼓励仅有链接的答案。请考虑 编辑您的答案 并在此处添加简介。 - Nazik
尊敬的 @NAZIK,我在这里回答问题并找到合适的解决方案来解决您的疑问! - Steve

0

让我们采取曲线点p1、p2、p3、p4和p5,并为每对相邻点找到中点。将m1标记为p1和p2的中点。同样地,对于m2、m3、m4也是这样。

  • 使用p2作为控制点向m2添加四次曲线。
  • 使用p3作为控制点向m3添加四次曲线。
  • 使用p4作为控制点向m4添加四次曲线。

代码:

CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;

UIView *aniView = [[UIView alloc] initWithFrame:CGRectMake(50, screenHeight, 50, 50)];
[aniView setBackgroundColor:[UIColor redColor]];
aniView.layer.cornerRadius = 25.0;
[self.view addSubview:aniView];


UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:aniView.center];
[movePath addQuadCurveToPoint:CGPointMake(screenWidth-50,screenHeight-50)
                 controlPoint:CGPointMake(screenWidth/2,screenHeight-150)];
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim,  nil];
animGroup.duration = 2.0;

[CATransaction begin]; {
    [CATransaction setCompletionBlock:^{
        [aniView.layer removeAllAnimations];
        [aniView removeFromSuperview];
    }];

    [aniView.layer addAnimation:animGroup forKey:nil];

} [CATransaction commit];

将上面的代码复制粘贴到某个方法中,然后尝试调用该方法...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接