Tag Archives: animation

Animating UIViews

I’ve been searching around for the best way to animate a button so that it would pulse continuously, and then how to stop it animating – without using the deprecated “commit” animation style. ┬áIt took some searching, so to save time next time, here is the way I settled on. The tricks are: there are options you can pass that keep the animation repeating, and autoreversing. There is also an option you need to pass if you want the button to still be pressable. And finally, to cancel the animation, just start a short new one which takes it back to its original size, which you must have saved somewhere beforehand. (Do not use an empty final animation though, as that doesn’t seem to stop the old one.)

[UIView animateWithDuration:0.3 delay:0
   options:(UIViewAnimationOptionCurveEaseInOut |
            UIViewAnimationOptionRepeat |
            UIViewAnimationOptionAutoreverse |
            UIViewAnimationOptionAllowUserInteraction)
   animations:^{
      CGFloat inset = -self.myButton.frame.size.width * .1;
      self.myButton.frame = CGRectInset(self.myButton.frame, inset, inset);
   }
   completion:NULL
];
[UIView animateWithDuration:0.15 delay:0
   options:(UIViewAnimationOptionBeginFromCurrentState |
            UIViewAnimationOptionCurveEaseInOut)
   animations:^{self.myButton.frame = self.origFrame;}
   completion:NULL];

One pitfall – I found if the button has an image, and it is already showing at full size, then the above code will not do anything – the image cannot be made larger than full size.