将简单动画转换为基于块的动画

ben*_*ink 2 iphone animation

我有一个动画,沿x轴稍微反弹一个视图.问题是使用旧的动画机制,并且作为文档状态建议iOS4 +使用基于块的动画.我想知道如何将这个简单的动画变成基于块的动画:

[UIView beginAnimations:@"bounce" context:nil];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

此外,我希望视图在动画结束后返回其起始位置,上面的方法反弹视图并将其x值增加10个像素.

谢谢你的帮助.

Kju*_*uly 5

基于块的动画的格式如下:

[UIView animateWithDuration:<#(NSTimeInterval)#>
                      delay:<#(NSTimeInterval)#>
                    options:<#(UIViewAnimationOptions)#>
                 animations:<#^(void)animations#>
                 completion:<#^(BOOL finished)completion#>];
Run Code Online (Sandbox Code Playgroud)

而下面的代码反弹的观点twicego back to origin position,或许存在更好的方法:

[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                 }
                 completion:^(BOOL finished) {
                     self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                     [UIView animateWithDuration:0.2f
                                           delay:0.0f
                                         options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                                      animations:^{
                                          self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                                      }
                                      completion:^(BOOL finished) {
                                          self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                                      }];
                 }];
Run Code Online (Sandbox Code Playgroud)

这一个是相似的,但是go back to origin position not smoothly.

[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                     self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                 }
                 completion:nil];
Run Code Online (Sandbox Code Playgroud)