贝塞尔曲线动画演示

经常写动画,好的动画效果,可以使应用的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"];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • 余观乎当今之世,修身树德效颜渊者寥寥;谋财求权如严嵩者泛泛。深乎痛哉,遂作此文以告诫之! 范文正公曰:“先天下之忧...
    西疆阅读 440评论 0 1
  • 当一台服务器有大量请求时,服务器会资源不足,导致客户端请求超时响应,返回502这样的错误。这个时候我们会想法增加服...
    金色的清晨阅读 312评论 0 0
  • 01 哐铛,可可的饭碗被打翻了,米饭洒落一地。可可抬头看着我,突然哭着大喊:“掉在地上的米饭还能吃吗?”我回答道:...
    沙拉随笔阅读 670评论 2 4
  • 文:剧不终 如果仔细盘点一下,我想,你大概和我一样吧:青春的记忆里,不知为何,盘踞着一个又一个和吃吃喝喝有关的故事...
    剧不终阅读 326评论 0 1