code_junkie

How do you explicitly animate a CALayer's backgroundColor

Nov 14th, 2011
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
  2. CGFloat values[4] = {1.0, 0.0, 0.0, 1.0};
  3. CGColorRef red = CGColorCreate(rgbColorspace, values);
  4. CGColorSpaceRelease(rgbColorspace);
  5.  
  6. CABasicAnimation *selectionAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
  7. [selectionAnimation setDuration:0.5f];
  8. [selectionAnimation setToValue:[NSValue valueWithPointer:red]];
  9. [layer addAnimation:selectionAnimation forKey:@"selectionAnimation"];
  10.  
  11. CABasicAnimation* selectionAnimation = [CABasicAnimation
  12. animationWithKeyPath:@"backgroundColor"];
  13. selectionAnimation.toValue = (id)[UIColor redColor].CGColor;
  14. [self.view.layer addAnimation:selectionAnimation
  15. forKey:@"selectionAnimation"];
  16.  
  17. CABasicAnimation *ba = nil;
  18.  
  19. ba = [CABasicAnimation animationWithKeyPath: kBackgroundColor];
  20.  
  21. self.paleRedColor = [UIColor colorWithRed: 127.0f/255.0f green: 0.0f blue: 0.0f alpha: 1.0f];
  22. ba.toValue = (id) paleRedColor.CGColor;
  23.  
  24. ba.repeatCount = 1;
  25. ba.autoreverses = YES;
  26. ba.duration = kDuration*2;
  27.  
  28. [self.view.layer addAnimation: ba forKey: @"flashColor"];
Add Comment
Please, Sign In to add comment