贝塞尔曲线动画演示

经常写动画,好的动画效果,可以使应用的level提升好几个档次,

下面有一个动画显得很舒服!个人表示很喜欢,在此做个记录。

贝塞尔曲线动画
//贝塞尔曲线动画
- (IBAction)clickAction:(UIButton *)sender {
    //图片的位置、大小(x,y,w,h)
    float imageX = 40;
    float imageY = 310;
    float imageW = 20;
    float imageH = 20;
    
    //把图片加到view上
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
    imageView.image = [UIImage imageNamed:@"cm_center_discount"];
    [self.view addSubview:imageView];
    
    CALayer *layer = [[CALayer alloc] init];
    layer.contents = imageView.layer.contents;
    layer.frame = imageView.frame;
    layer.opacity = 1;
    [self.view.layer addSublayer:layer];
    
    //都以self.view为参考系
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //动画起点
    CGPoint startPoint = CGPointMake(imageX+imageW/2, imageY+imageH/2);
    [path moveToPoint:startPoint];
    
    //动画终点
    CGPoint endpoint = CGPointMake(320, 568);
    
    //贝塞尔曲线中间点
    float sx = startPoint.x;
    float sy = startPoint.y;
    float ex = endpoint.x;
    float ey = endpoint.y;
    float x = sx+(ex-sx)/3;
    float y = sy+(ey-sy)*0.5-400;
    CGPoint centerPoint = CGPointMake(x,y);
    [path addQuadCurveToPoint:endpoint controlPoint:centerPoint];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = 0.7;
    animation.delegate = self;
    animation.autoreverses = YES;//是否自动退回
    animation.removedOnCompletion = NO;//完成后是否移除
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [layer addAnimation:animation forKey:@"buy"];
}

你可能感兴趣的:(贝塞尔曲线动画演示)