Mais conteúdo relacionado Learning Core Animation4. Layer
• The key of Core Animation
• Merely manages the state information surrounding a
bitmap
• Consider as model in MVC
13年6月3日星期⼀一
7. Transform
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
13年6月3日星期⼀一
10. Using an image
CALayer *layer = someView.layer;
layer.contents = (id)someCGImage;
layer.contentScale = kScreenScale;
13年6月3日星期⼀一
11. Using a delegate
- (void)displayLayer:(CALayer *)theLayer {
// Check the value of some state property
if (self.displayYesImage) {
// Display the Yes image
theLayer.contents = [someHelperObject loadStateYesImage];
}
else {
// Display the No image
theLayer.contents = [someHelperObject loadStateNoImage];
}
}
13年6月3日星期⼀一
12. Using a delegate
- (void)drawLayer:(CALayer *)theLayer inContext:
(CGContextRef)theContext {
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
CGPathAddCurveToPoint(thePath,
NULL,
15.f,250.0f,
295.0f,250.0f,
295.0f,15.0f);
CGContextBeginPath(theContext);
CGContextAddPath(theContext, thePath);
CGContextSetLineWidth(theContext, 5);
CGContextStrokePath(theContext);
CFRelease(thePath);
}
13年6月3日星期⼀一
13. Layer’s appearance
@property(copy) NSString *contentsGravity;
@property CGFloat contentsScale;
@property CGColorRef backgroundColor;
@property CGFloat cornerRadius;
@property CGFloat borderWidth;
@property CGColorRef borderColor;
@property float opacity;
@property(copy) NSArray *filters;
@property CGColorRef shadowColor;
@property float shadowOpacity;
@property CGSize shadowOffset;
13年6月3日星期⼀一
18. Using path
- (void)animatePosition {
CAKeyframeAnimation *theAnimation =
[CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path = someCGPath;
theAnimation.duration = 5.0;
[theLayer addAnimation:theAnimation forKey:@"position"];
}
13年6月3日星期⼀一
20. Specify values and timing
- (void)animateBorder {
CAKeyframeAnimation *widthAnimation = [CAKeyframeAnimation
animationWithKeyPath:@"borderWidth"];
NSArray *widthValues = @[@1.0, @10.0, @5.0, @0.0];
widthAnimation.values = widthValues;
widthAnimation.calculationMode = kCAAnimationPaced;
widthAnimation.keyTimes = @[@0.0f, @0.66667f, @1.0f];
[theLayer addAnimation:widthAnimation forKey:@"borderWidth"];
}
13年6月3日星期⼀一
21. Multiple animations
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[animation0, animation1];
group.duration = 5.0;
[layer addAnimation:group forKey:@"groupedAnimations"];
13年6月3日星期⼀一
23. Stop an animation
[theLayer addAnimation:theAnimation forKey:@"animationKey"];
// somedays later
[theLayer removeAnimationForKey:@"animationKey"];
13年6月3日星期⼀一
24. Detecting animation’s end
// invoking in the delegate
- (void)animationDidStart:(CAAnimation *)theAnimation
- (void)animationDidStop:(CAAnimation *)theAnimation finished:
(BOOL)flag
13年6月3日星期⼀一