我如何移动一个UIView到一个结束位置,但让它在接近它时慢下来



我试图移动一个盒子从点a =(0,0)到B =(0,1000),但我想慢下来,因为它接近点B.我尝试UIViewAnimationOptionCurveEaseOut但它似乎是在一个恒定的速度移动。

UIView *box = [[UIView alloc] init];
box.frame = CGRectMake(0, 0, 500, 500);
box.backgroundColor = [UIColor redColor];
[self.view addSubview:box];
// Animation
[UIView animateWithDuration:0.3
                  delay:0.0
                options:UIViewAnimationOptionCurveEaseOut
             animations:^{
                 box.frame = CGRectMake(0, 1000, box.frame.size.width, box.frame.size.height);
             } completion:nil];

任何想法?

最简单的解决方案是用不同的速度在多个阶段制作动画,例如:

// Animation stage 1
[UIView animateWithDuration:0.1 //First half is fast
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     box.frame = CGRectMake(0, 500, box.frame.size.width, box.frame.size.height);
                 } completion:^(BOOL finished) {
                     // Animation stage 2
                     [UIView animateWithDuration:0.2 //Second half slower
                                           delay:0.0
                                         options:UIViewAnimationOptionCurveEaseOut
                                      animations:^{
                                          box.frame = CGRectMake(0, 1000, box.frame.size.width, box.frame.size.height);
                                      } completion:nil];
                 }];

应该可以得到一个平滑的动画,通过玩弄它一点。

如果这是iOS 7+,使用spring动画。将此代码添加到viewDidAppear:中进行测试,并查看使用弹簧与简单的缓出曲线之间的区别。

还需要注意的是,苹果确实鼓励使用spring动画,因为iOS 7+中几乎所有(或全部?)的动画都是用spring完成的。

UIView *fooView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 50, 50)];
fooView.backgroundColor = [UIColor redColor];
[self.view addSubview:fooView];
[UIView animateWithDuration:1
                      delay:1
     usingSpringWithDamping:1
      initialSpringVelocity:1
                    options:0
                 animations:^{
                     fooView.frame = CGRectMake(50, 400, 50, 50);
                 }
                 completion:nil];
UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(200, 100, 50, 50)];
barView.backgroundColor = [UIColor redColor];
[self.view addSubview:barView];
[UIView animateWithDuration:1
                      delay:1
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     barView.frame = CGRectMake(200, 400, 50, 50);
                 }
                 completion:nil];

如果您不满意内置的缓和函数,您可以创建自己的。你想要一些与CAMediaTimerFunction或CAKeyframeAnimation相关的东西。你可以在这里和这里读到。这里也有一些很好的答案

最新更新