Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Core Animation for the Masses

corvino
August 17, 2015

Core Animation for the Masses

corvino

August 17, 2015
Tweet

More Decks by corvino

Other Decks in Programming

Transcript

  1. #define DURATION (1.2) @interface ViewController () { NSTimer *animationTimer; NSInteger

    animationCount; NSInteger numberIterations; CGPoint startPosition; CGPoint endPosition; } @end
  2. @implementation ViewController - (IBAction)doAnimation:(id)sender { animationTimer = [NSTimer scheduledTimerWithTimeInterval:1./60. target:self

    selector:@selector(updateAnimation:) userInfo:nil repeats:YES]; animationCount = 0; numberIterations = 60. * DURATION; startPosition = self.targetView.frame.origin; if (50. == startPosition.x) { endPosition = self.bottomRight; } else { endPosition = CGPointMake(50., 50.); } }
  3. - (void)updateAnimation:(id)timer { animationCount++; if (numberIterations == animationCount) { [animationTimer

    invalidate]; } CGRect targetFrame = self.targetView.frame; CGFloat endWeight = 1. * animationCount / numberIterations; CGFloat startWeight = 1. - endWeight; targetFrame.origin = CGPointMake( startWeight * startPosition.x + endWeight * endPosition.x, startWeight * startPosition.y + endWeight * endPosition.y); self.targetView.frame = targetFrame; } @end
  4. Core Animation @implementation ViewController - (IBAction)doAnimation:(id)sender { [UIView animateWithDuration:1.2 delay:0.

    options:UIViewAnimationOptionCurveLinear animations:^{ CGRect targetFrame = self.targetView.frame; if (50. == targetFrame.origin.x) { targetFrame.origin = self.bottomRight; } else { targetFrame.origin = CGPointMake(50., 50.); } self.targetView.frame = targetFrame; } completion:^(BOOL finished) {}]; } @end
  5. Core Animation @implementation ViewController - (IBAction)doAnimation:(id)sender { [UIView animateWithDuration:1.2 animations:^{

    CGRect targetFrame = self.targetView.frame; if (50. == targetFrame.origin.x) { targetFrame.origin = self.bottomRight; } else { targetFrame.origin = CGPointMake(50., 50.); } self.targetView.frame = targetFrame; }]; } @end
  6. Visual Effects • backgroundColor • backgroundFilters • borderColor • borderWidth

    • contentsCenter • contentsGravity • cornerRadius • hidden • mask • masksToBounds • opacity • shadowColor • shadowOpacity • shadowPath • shadowRadius
  7. UIView Animations [UIView animateWithDuration:1.0 animations:^{ … }]; [UIView animateWithDuration:1.2 delay:0.

    options:UIViewAnimationOptionCurveEaseInOut animations:^{ … } completion:^(BOOL finished) { … }];
  8. CGRect targetFrame = self.targetView.frame; CGFloat endWeight = 1. * animationCount

    / numberIterations; CGFloat startWeight = 1. - endWeight; targetFrame.origin = CGPointMake( startWeight * startPosition.x + endWeight * endPosition.x, startWeight * startPosition.y + endWeight * endPosition.y); self.targetView.frame = targetFrame; Interpolation
  9. Implicit Animations • CALayer: • - (NSDictionary *) actions •

    CATransaction • + setDisableActions:(BOOL)
  10. CABasicAnimation CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"]; theAnimation.fromValue = [NSValue valueWithCGRect:self.targetView.bounds];

    theAnimation.toValue = [NSValue valueWithCGRect:newBounds]; theAnimation.duration = 1.2; [self.targetView.layer addAnimation:theAnimation forKey:@"AnimateFrame"]; self.targetView.bounds = newBounds;
  11. CAKeyframeAnimation CGImageRef images[N]; // load the images... CAKeyframeAnimation *animation =

    [CAKeyframeAnimation animation]; animation.keyPath = @"contents"; animation.duration = 1; animation.calculationMode = kCAAnimationDiscrete; animation.repeatCount = HUGE_VALF; animation.values = @[(__bridge id)images[1], (__bridge id)images[2] /* , ... */ ]; [layer addAnimation:animation forKey:@"slideshow"];
  12. CALayer Subclasses • CAShapeLayer • CATiledLayer • CATextLayer • AV

    Layers (CACaptureVideoPreviewLayer, AVPlayerLayer, etc.) • CAGradientLayer • CAEmitterLayer • CAEAGLLayer
  13. CAShapeLayer CAShapeLayer *shape = [CAShapeLayer layer]; shape.frame = someFrame; shape.strokeColor

    = [UIColor greenColor].CGColor; shape.lineWidth = 3.; shape.fillColor = [UIColor yellowColor].CGColor; shape.path = CGPathCreateWithEllipseInRect( CGRectMake(0., 0., someFrame.size.width, someFrame.size.height), &CGAffineTransformIdentity);
  14. CATiledLayer SomeTiledLayerDelegate *tiledDelegate = [[SomeTiledLayerDelegate alloc] init]; CATiledLayer *tiledLayer =

    [CATiledLayer layer]; tiledLayer.delegate = tiledDelegate; tiledLayer.frame = CGRectMake(...); tiledLayer.contentsScale = [[UIScreen mainScreen] scale]; @implementation SomeTiledLayerDelegate - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context { CGRect box = CGContextGetClipBoundingBox(context); // ... } @end