Add a pulsating image to a new CALayer centred on an existing view.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Somewhere in the setup code, possible awakeFromNib if relevant. NOT in drawRect:! UIImage *img = [UIImage imageNamed:@"BigHand"]; UIGraphicsBeginImageContext(img.size); [img drawAtPoint:CGPointMake(0, 0)]; // handy! CALayer *newLayer = [CALayer layer]; newLayer.bounds = CGRectMake(0, 0, img.size.width, img.size.height); newLayer.position = CGPointMake(self.layer.bounds.size.width/2.0, self.layer.bounds.size.height/2.0); // centred newLayer.contentsScale = self.layer.contentsScale; // for retina compat. Make sure you've set you view's main layer newLayer.contents = (__bridge id)UIGraphicsGetImageFromCurrentImageContext().CGImage; newLayer.opacity = 1.0; UIGraphicsEndImageContext(); // Create the animation... CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"]; anim.fromValue = [NSNumber numberWithFloat:0.6]; anim.toValue = [NSNumber numberWithFloat:1.0]; anim.duration = 0.7; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; anim.autoreverses = YES; anim.repeatCount = HUGE_VALF; // repeat forever [newLayer addAnimation:anim forKey:@"opacity"]; [self.layer addSublayer:newLayer]; |
