UIButton的动画效果

3

是否可以对UIButton进行动画处理?

我想要的效果是让按钮像云一样在一个给定的区域内来回移动。

我已经尝试过对UIImages进行动画处理,但不知道UIButton是否也可以被动画化。

2个回答

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

3

UIView 实例应该可以通过 [UIView animateWithDuration:animations:] 进行动画处理。由于 UIButtonUIView 的子类,我不预见会有任何问题...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接