Crisn
Crisn

Reputation: 243

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.

Upvotes: 3

Views: 2028

Answers (2)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

As already said UIButton instance should be animatable as its a subclass of UIView. The below code will move your UIButton back-forth i.e. 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)
         {
         }
     }];
}

Upvotes: 3

barley
barley

Reputation: 4463

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

Upvotes: 3

Related Questions