Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to Move UIViews using Core Animation in iOS?

$
0
0

Core Animation based on Quartz provides very simple ways to create special effects in your game or application.

The following shows how to create a Move Effect of any UIView (e.g. UIButtonView, UIImageView etc.)

  1. +(void)move:(UIView*)view to:(CGPoint)point withDuration:(int) duration andSnapBack:(BOOL) snapBack
  2. {
  3.     // Prepare the animation from the current position to the new position
  4.     CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"position"];
  5.     animation.fromValue =[view.layer valueForKey:@"position"];
  6.    
  7.    
  8.     // iOS
  9.     animation.toValue =[NSValue valueWithCGPoint:point];
  10.     animation.duration = duration;
  11.    
  12.     if(!snapBack)
  13.     {
  14.         // Update the layer's position so that the layer doesn't snap back when the animation completes.
  15.         view.layer.position = point;
  16.     }
  17.    
  18.     // Add the animation, overriding the implicit animation.
  19.     [view.layer addAnimation:animation forKey:@"position"];
  20. }

Viewing all articles
Browse latest Browse all 178

Trending Articles