2011-12-11 47 views
2

我有一个动画,沿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个像素。

感谢您的任何帮助。

回答

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]; 
+0

谢谢@Kjuly您提供的中间代码块做了诀窍:) – bennythemink

+0

@bennythemink u'r welcome :) – Kjuly

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]; 

这应该这样做,但我不认为有一种方法来指定自动重复计数。

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) { 
}]; 

附:我还没有尝试过...

+0

谢谢阿里尔,但这会重复动画永远。我猜我可以放入一些'if'语句检查,但@ Kjuly的答案让我只用动画块来做。反正给了你一个投票给你的麻烦:) – bennythemink