2013年2月3日 星期日

CALayer


-(void)setupCALayer{
    UIImage* image1 = [UIImage imageNamed:@"image10.jpg"];
    CALayer* myLayer = [CALayer layer];
    myLayer.bounds = CGRectMake(0, 0, 200, 150);
    myLayer.position = CGPointMake(100, 75);
    myLayer.contents = (id)image1.CGImage;
    [self.view.layer addSublayer:myLayer];
    
    CAShapeLayer* shapeLayer = [CAShapeLayer layer];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGPathAddRect(pathRef,nil, CGRectMake(0, 0, 200, 50));
    shapeLayer.bounds = CGRectMake(0, 0, 200, 50);
    shapeLayer.position = CGPointMake(100, 125);
    shapeLayer.path = pathRef;
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    shapeLayer.opacity = 0.6;
    [myLayer addSublayer:shapeLayer];
    
    CATextLayer* textLayer = [CATextLayer layer];
    textLayer.string = @"hello";
    textLayer.font = [UIFont systemFontOfSize:8].fontName;
    textLayer.fontSize = 24;
    textLayer.foregroundColor = [UIColor blackColor].CGColor;
    textLayer.bounds = CGRectMake(0, 0, 200, 50);
    textLayer.position = CGPointMake(100, 25);
    [shapeLayer addSublayer:textlayer];
}


- (IBAction)onButtonClick:(id)sender {
    [CATransaction begin];
    [CATransaction setAnimationDuration:5.0];
    myLayer.position = CGPointMake(100, 275);
    shapeLayer.fillColor = [UIColor greenColor].CGColor;
    textLayer.fontSize=36;
    [CATransaction commit];
}



- (IBAction)onTransformClick:(id)sender {
    [CATransaction setAnimationDuration:3.0];
    myLayer.affineTransform = CGAffineTransformMakeRotation(M_PI/2);
    myLayer.affineTransform = CGAffineTransformScale(myLayer.affineTransform, 2.0, 1.5);
}


- (IBAction)shadow:(id)sender {
    myLayer.shadowOpacity = 1.0;
    myLayer.shadowOffset = CGSizeMake(10, 100);
    myLayer.shadowColor = [UIColor blueColor].CGColor;
    myLayer.shadowRadius = 15;
    //設定pathCALayer的矩形,效果一樣但會加速
    myLayer.shadowPath = [UIBezierPath bezierPathWithRect:myLayer.bounds].CGPath;
}

//真正的動畫是不會動到基本資料的

- (IBAction)onAnimDown:(id)sender {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position.y"];
    anim.toValue = @300; //anim.toValue = [NSNumber numberWithInt:300];
    anim.duration = 5;
    [myLayer addAnimation:anim forKey:@"downAnim"];
}

- (IBAction)onAnimUp:(id)sender {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position.y"];
    anim.toValue = @50;
    anim.duration = 5;
    [myLayer addAnimation:anim forKey:@"downUp"];
}
- (IBAction)onCancelDown:(id)sender {
    [myLayer removeAnimationForKey:@"downAnim"];
    [myLayer removeAllAnimations];
}

- (IBAction)onTwinkleClick:(id)sender {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    anim.toValue = @0.1;
    anim.repeatCount = 5;
    anim.autoreverses = YES;
    anim.duration = 1.5;
    [myLayer addAnimation:anim forKey:@"twinkle"];
}


- (IBAction)onFlyClick:(id)sender {
    CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, nil, 100, 75);
    
    CGPathAddQuadCurveToPoint(pathRef, nil, 600, -50, 300, 300);
    anim.path = pathRef;
    anim.duration = 8.0;
    //設定代理物件
    anim.delegate = self;
    [myLayer addAnimation:anim forKey:@"fly"];
    

}

-(void)animationDidStart:(CAAnimation *)anim{
    myLayer.shadowOpacity = 1;
    myLayer.shadowColor = [UIColor redColor].CGColor;
    myLayer.shadowOffset= CGSizeMake(10,10);
    
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    myLayer.shadowOpacity = 0;
}


沒有留言:

張貼留言