[IOS筆記] - 動畫animation

//移動
- (IBAction)translation:(id)sender {
    CABasicAnimation *traslation = [CABasicAnimation animationWithKeyPath:@"position"];
    traslation.toValue = [NSValue valueWithCGPoint:CGPointMake(320,480)];
    traslation.duration = 2.0;
    //traslation.autoreverses = YES;
    traslation.repeatCount = 1;

    [self.m_image.layer addAnimation:traslation forKey:@"traslation"];
}

//透明
- (IBAction)opacity:(id)sender {
    CABasicAnimation *opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacity.fromValue = [NSNumber numberWithFloat:1.0];
    opacity.toValue = [NSNumber numberWithFloat:0.4];
    opacity.duration = 0.2; //動畫時間
    opacity.repeatCount = FLT_MAX; //永久
    opacity.autoreverses = YES; //每次動畫後倒回回放
    opacity.removedOnCompletion=NO;  //動畫後不還原,爲no時不回到最初狀態
    opacity.fillMode=kCAFillModeForwards;
    
    [self.m_image.layer addAnimation:opacity forKey:@"opacity"];
}

// 旋轉
- (IBAction)rotate:(id)sender {
    CATransform3D ca3d = CATransform3DMakeRotation(45 * 3.14159265/180.0, -1, 1, 1);
    
    CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.toValue = [NSValue valueWithCATransform3D:ca3d];
    rotate.duration=1.0;
    rotate.autoreverses=NO;
    rotate.repeatCount=1;
    rotate.removedOnCompletion=NO;
    rotate.fillMode=kCAFillModeForwards;
    [self.m_image.layer addAnimation:rotate forKey:@"rotate"];
}

- (IBAction)alpha:(id)sender {
}

//縮放
- (IBAction)scale:(id)sender {
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue=[NSNumber numberWithFloat:0.5];
    scale.toValue = [NSNumber numberWithFloat:2.0];
    scale.duration=1.0;
    scale.autoreverses=YES;
    scale.repeatCount=2;
    scale.removedOnCompletion=YES;
    scale.fillMode=kCAFillModeForwards;
    [self.m_image.layer addAnimation:scale forKey:@"scale"];
}

//不按原始邊長度縮放
-(IBAction)bounds:(id)sender{
    CABasicAnimation *bounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
    bounds.duration = 1.f;
    bounds.fromValue = [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];
    bounds.toValue = [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];
    bounds.byValue  = [NSValue valueWithCGRect:self. m_image.bounds];

    bounds.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    bounds.repeatCount = 1;
    bounds.autoreverses = YES;
    
    [self.m_image.layer addAnimation:bounds forKey:@"bounds"];
}

- (IBAction)path:(id)sender {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, 0);
    //添加直線路徑
    CGPathAddLineToPoint(path, NULL, 60, 130);
    CGPathAddLineToPoint(path, NULL, 70, 140);
    CGPathAddLineToPoint(path, NULL, 80, 150);
    CGPathAddLineToPoint(path, NULL, 90, 160);
    CGPathAddLineToPoint(path, NULL, 100, 170);
    //添加曲線路徑
    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
    
    animation.path = path;
    animation.duration = 5;
    animation.autoreverses = YES;
    [self.m_image.layer addAnimation:animation forKey:@"path"];
    CFRelease(path);
}
//組合動畫
- (IBAction)goup:(id)sender {
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue=[NSNumber numberWithFloat:0.5];
    scale.toValue = [NSNumber numberWithFloat:2.0];
    
    CABasicAnimation *traslation = [CABasicAnimation animationWithKeyPath:@"position"];
    traslation.toValue = [NSValue valueWithCGPoint:CGPointMake(320,480)];
    
    group.animations=[NSArray arrayWithObjects:scale, traslation, nil];
    group.duration = 2.0;
    
    [self.m_image.layer addAnimation:group forKey:@"group"];
}

 

參考連接:html

http://blog.csdn.net/huifeidexin_1/article/details/8504075ios

http://www.cnblogs.com/mjios/archive/2013/04/15/3021343.htmlide

相關文章
相關標籤/搜索