简体   繁体   中英

Animating UIButton

Is it possible to animate a UIButton ?

What I want to do with my button is to animate it like a cloud where it moves back and forth in a given area only.

I've tried animating UIImages but I don't know if UIButton can be animated too.

UIView instance should be animatable with [UIView animateWithDuration:animations:] . Since UIButton is a subclass of UIView , I don't foresee any problem...

As already said UIButton instance should be animatable as its a subclass of UIView . The below code will move your UIButton back-forth ie left-right 20 pixels for 10 times. Basically I am chaining 2 animations together.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM