动画UIButton

时间:2012-02-11 03:00:28

标签: objective-c ios uibutton core-animation

是否可以为UIButton设置动画?

我想用我的按钮做的就是将它设置为像云一样的动画,它只在给定区域内来回移动。

我已尝试动画UIImages,但我不知道UIButton是否也可以设置动画。

2 个答案:

答案 0 :(得分:3)

UIView实例应该可以使用[UIView animateWithDuration:animations:]设置动画。由于UIButtonUIView的子类,我预见不会有任何问题......

答案 1 :(得分:3)

正如已经说过UIButton实例应该是可动画的,因为它是UIView的子类。以下代码将移动UIButton来回,即左右20像素,持续10次。基本上我在一起链接2个动画。

- (void)startLeftRightAnimation
{
[UIView animateWithDuration:0.5 
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseIn 
                  animations:^(void) 
     {
         [self.button setFrame:CGRectMake(self.logo.frame.origin.x-20,self.logo.frame.origin.y,self.logo.frame.size.width,self.logo.frame.size.height)];
     } 
                  completion:^(BOOL finished) 
     {
         if(finished)
         {
             [UIView animateWithDuration:0.5 
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseIn 
                  animations:^(void) 
             {
                 [self.button setFrame:CGRectMake(self.logo.frame.origin.x+40,self.logo.frame.origin.y,self.logo.frame.size.width,self.logo.frame.size.height)];
                 [self startLeftRightAnimation];
             }
                  completion:^(BOOL finished) 
         {
         if(finished)
         {
         }
     }];
}