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

时间:2011-12-11 12:34:54

标签: 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];

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

感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

基于块的动画格式如下:

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

下面的代码会反映视图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);
                                      }];
                 }];

这个类似,但是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];

答案 1 :(得分:2)

试试这段代码:

[UIView setAnimationRepeatCount:2];
[UIView animateWithDuration:0.2 
                      delay:0.2 
                    options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) 
                 animations:^{
                     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                 } completion:^(BOOL finished) {
}];

P.S。我没试过......

答案 2 :(得分:0)

     [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAutoreverse                 animations:^{
     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
     } completion:nil];

这应该这样做,但我认为没有办法指定自动重复计数。