Breaking News
Home / iOS / iOS Snippets / How to Move UIViews using Core Animation in iOS?

How to Move UIViews using Core Animation in iOS?

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. }
[pb_builder]

Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Leave a Reply