iOS Core Animation

33
Core Anima+on Jussi Pohjolainen

Transcript of iOS Core Animation

Page 1: iOS Core Animation

Core  Anima+on  

Jussi  Pohjolainen  

Page 2: iOS Core Animation

UIVIEW  ANIMATION  

Page 3: iOS Core Animation

UIView  Anima+ons  

•  Several  view  proper+es  can  be  animated  – Anima+on:  change  property  over  +me  

•  UIView  does  most  of  the  job  •  Two  ways  – Block-­‐based  anima:ons  (recommended)  – Begin/commit  anima+ons  

 

Page 4: iOS Core Animation

Animatable  Proper+es  

•  frame  •  bounds  •  center  •  transform  – Rota+on,  Scale,  Move..  

•  alpha  •  backgroundColor  •  contentStretch  

Page 5: iOS Core Animation

UIView  Block  Methods  for  Anima+on  Animate  changes  to  one  or  more  views  using  the  specified  dura5on,  delay,  op5ons,  and  comple5on  handler. + animateWithDuration:delay:options:animations:completion:

Animate  changes  to  one  or  more  views  using  the  specified  dura5on  and  comple5on  handler.  + animateWithDuration:animations:completion:

Animate  changes  to  one  or  more  views  using  the  specified  dura5on. + animateWithDuration:animations:

Creates  a  transi5on  anima5on  for  the  specified  container  view. + transitionWithView:duration:options:animations:completion:

Creates  a  transi5on  anima5on  between  the  specified  views  using  the  given  parameters. + transitionFromView:toView:duration:options:completion:

Page 6: iOS Core Animation

Op+ons  

•  Lot  of  anima+on  op+ons  – CurveEaseIn,  out,  linear  – Repeat..  

•  Can  be  combined  using  |  – UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse

Page 7: iOS Core Animation

Anima+on  with  Blocks  - (void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

[UIView animateWithDuration:5.0

animations:^{

[animatingView setAlpha:0];

[animatingView setCenter:CGPointMake(animatingView.center.x+50.0,

animatingView.center.y+50.0)];

}

completion:^(BOOL finished) {

[animatingView removeFromSuperview];

}

];

}

Page 8: iOS Core Animation

CORE  ANIMATION  

Page 9: iOS Core Animation

Core  Anima+on  Layer    

•  Anima+on  is  an  important  part  of  iOS    •  Add  QuartzCore  framework  to  your  project  •  Use  classes    – CALayer  • When  drawing  using  layers,  drawing  is  hardware-­‐accelerated  •  Layer  is  a  buffer  containing  a  bitmap  

– CAAnima+on  

 

Page 10: iOS Core Animation

Implicit  Layer  

•  Every  UIView  has  an  CALayer  – When  UIView  draws  itself,  the  view  image  is  in  CALayer  

•  View  creates  a  layer:  implicit  layer  – Matching  view  hierarchy  and  layer  hierarchy  

Page 11: iOS Core Animation

Explicit  Layer  

•  To  create  a  explicit  layer,  just  create  instance  of  CALayer  

•  Layer  has  bounds  and  posi+on  •  Posi+on  is  by  default  the  center  of  the  layer.  Uses  coordinates  of  the  superlayer’s  coordinate  system  

Page 12: iOS Core Animation

Explicit  Layer   CALayer* layer = [[CALayer alloc] init];

// Setting bounds to layer

CGRect bounds = CGRectMake(0.0, 0.0, 100.0, 100.0);

[layer setBounds:bounds];

// Setting *center* position to layer (uses superlayer's coordinate system)

CGPoint position = CGPointMake(50.0, 150.0);

[layer setPosition:position];

// Setting color to layer

UIColor* red = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];

// Cast from UIKit to Core Graphics

CGColorRef redRef = [red CGColor];

[layer setBackgroundColor:redRef];

[[[self view] layer] addSublayer: layer];

Page 13: iOS Core Animation

UIColor,  CGColorRef  UIImage,  CGImageRef?  

•  QuartzCore  frame  and  Core  Graphics  framework  eist  on  both  iOS  and  Mac  OS  X  

•  UIKit  exists  only  on  iOS  – UIImage,  UIColor  …  

•  Due  to  portability  QuartzCore  uses  classes  like  – CGImageRef,  CGColorRef  

•  UIKit  provides  methods  to  access  Core  Craphics  counterparts  

Page 14: iOS Core Animation

Using  Image   // Create a UIImage UIImage* layerImage = [UIImage imageNamed:@"dude.png"]; // Get underlying CGImage CGImageRef image = [layerImage CGImage]; // CGImageRef => typecast struct CGImage* // To cast from C-pointer to Objective-C id, we use // __bridge [layer setContents: (__bridge id)(image)]; // Fill the layer and maintain the aspect ratio [layer setContentsGravity:kCAGravityResizeAspect]; [[[self view] layer] addSublayer: layer];

Page 15: iOS Core Animation

ZPosi+on  

•  Layers  exist  in  hierarchy  – Can  have  sublayers  – Each  layer  has  pointer  back  to  its  parent:  superlayer  

•  Layer  draws  on  top  of  its  superlayer  •  What  about  siblings  and  overlapping?  – Layer  has  property  zPosi+on  –  If  siblings  overlap,  layer  with  higher  zPosi+on  is  composed  on  top.  

Page 16: iOS Core Animation

Example  // Layer 2 will be on top of layer1

[layer1 setZPosition:0.0];

[layer2 setZPosition:1.0];

// Set background color to layer2

UIColor* red = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];

CGColorRef redRef = [red CGColor];

[layer2 setBackgroundColor:redRef];

// add layers as siblings

[[[self view] layer] addSublayer: layer1];

[[[self view] layer] addSublayer: layer2];

Page 17: iOS Core Animation

Implicitly  Animatable  Proper+es  

•  Several  CALayer  proper+es  are  implicitly  animatable  – When  a  se?er  is  called,  proper5es  are  animated!  – Calling  setPosi+on  will  animate  layer  from  a  to  b  

•  The  animatable  property  changes  to  its  des+na+on  value  over  constant  +me  interval  

•  Changing  property  while  being  animated  starts  another  anima+on:  anima+on  seems  slow  

 

Page 18: iOS Core Animation
Page 19: iOS Core Animation

Disable  implicit  anima+on  

•  To  disable  implicit  anima+on,  use  anima+on  transac+on  

•  Batch  anima+ons  and  set  there  parameters,  like  dura+on  and  anima+on  curve  

•  Set  begin  and  commit  to  CATransac+on  

Page 20: iOS Core Animation
Page 21: iOS Core Animation

CAANIMATION  

Page 22: iOS Core Animation

About  Anima+on  

•  Anima+on  object  changes  over  :me  •  Instruc+on  set  that  can  be  added  to  a  CALayer  instance  

•  When  instruc+ons  are  added  to  layer,  layer  begins  following  the  instruc+ons  of  the  anima+on.  

•  Many  proper+es  can  be  animated:  opacity,  posi+on,  transform,  bounds  …  

Page 23: iOS Core Animation

Anima+on  Classes  

•  CAAnima+on  (Abstract)  – CAPropertyAnima+on  (Abstract)  •  CABasicAnima:on  •  CAKeyFrameAnima:on  

– CAAnima:onGroup  – CATransi:on  

Page 24: iOS Core Animation

CABasicAnima+on  

•  CABasicAnima+on  has  two  values  – fromValue – toValue –  (and  the  dura+on  from  CAAnimation)  

•  Decide  the  property,  then  set  values  fromValue  to  toValue  and  set  dura+on  also  

Page 25: iOS Core Animation

Spinning  Layer  Example  // Create CABasicAnimation and inform about the property // we are going to influence CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; // Changes rotation (uses radians) [spin setToValue:[NSNumber numberWithFloat:M_PI*2.0]]; // Set duration to one second [spin setDuration:1.0]; // Repeat forever [spin setRepeatCount:HUGE_VALF]; // Add the animation to layer [_layer1 addAnimation:spin forKey:@"spinAnimation"];

Page 26: iOS Core Animation
Page 27: iOS Core Animation

CAKeyframeAnima+on  

•  The  basic  anima+on  animates  from  one  value  to  second.    

•  CAKeyFrameAnima+on  accepts  many  values,  an  NSArray  of  values.  

•  The  NSArray  is  set  as  the  values  property  of  a  CAKeyframeAnima+on  

Page 28: iOS Core Animation

Move  Layer  Example   CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation

animationWithKeyPath:@"position"];

NSMutableArray* locations = [NSMutableArray array];

// C API (CGPoint is struct)

CGPoint loc = CGPointMake(0.0, 0.0);

// Cast to Objective-C object

NSValue* value = [NSValue valueWithCGPoint:loc];

[locations addObject: value];

[locations addObject: [NSValue valueWithCGPoint:CGPointMake(150,150)]];

[locations addObject: [NSValue valueWithCGPoint:CGPointMake(100,350)]];

[moveAnim setValues: locations];

[moveAnim setDuration:5.0];

[moveAnim setRepeatCount:HUGE_VALF];

Page 29: iOS Core Animation
Page 30: iOS Core Animation

CAAnima+onGroup  

•  CAAnima+onGroup  holds  group  of  CAAnima+ons  

•  Several  anima+ons  done  concurrently  •  Add  NSArray  of  anima+ons  to  CAAnima+onGroup  and  set  this  to  layer  

Page 31: iOS Core Animation

CAAnima+onGroup   CAKeyframeAnimation *move = [CAKeyframeAnimation animationWithKeyPath:@"position"]; ... CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; [spin setToValue:[NSNumber numberWithFloat:M_PI*2.0]]; [spin setDuration:1.0]; [spin setRepeatCount:HUGE_VALF]; CAAnimationGroup* group = [CAAnimationGroup animation]; [group setAnimations:[NSArray arrayWithObjects:spin, move, nil]]; [group setDuration:5.0]; [group setRepeatCount:HUGE_VALF];

Page 32: iOS Core Animation
Page 33: iOS Core Animation

CATransi+on  

•  Transi+on  of  layer  on  and  off  screen  •  UINaviga+onController  uses  CATransi+on  •  Set  type  – Fade,  MoveIn,  Push,  Reveal  

•  Set  subtype  (direc+on)  – Le`,  right,  up,  boaom  

•  And  set  also  dura+on