iOS 贝塞尔曲线动画简单实现

-(void)quadCurveMove
{
      UIView * roundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
      roundView.backgroundColor = RandColor;
      [roundView addRadius:10];
      [self.roundViewArray addObject:roundView];
      roundView.x = (self.width - roundView.width)+roundView.width*0.5;;
      roundView.y = (self.height - roundView.height)-roundView.height*0.5;
      [self addSubview:roundView];
      //起始点
      CGPoint startPoint= CGPointMake(roundView.x, roundView.y);
     //结束点
       CGPoint endPoint=     CGPointMake(self.countLabel.x+self.countLabel.width*0.5,    self.countLabel.y+self.countLabel.height*0.5);
    //控制点
      CGPoint controlPoint = CGPointMake(endPoint.x+((startPoint.x-endPoint.x))*0.5, -100);
    
      //绘制贝塞尔曲线
      UIBezierPath * movePath = [UIBezierPath bezierPath];
      [movePath moveToPoint:startPoint];
      [movePath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    //关键帧动画
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [keyFrameAnimation setDuration:0.4];
    keyFrameAnimation.path = movePath.CGPath;
    keyFrameAnimation.fillMode = kCAFillModeForwards;
    keyFrameAnimation.removedOnCompletion = NO;
    keyFrameAnimation.delegate = self;
    [roundView.layer addAnimation:keyFrameAnimation forKey:@"movingAnimation"];
}

//动画结束调用该方法
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    self.count+=1;
    self.countLabel.text=[NSString stringWithFormat:@"%ld",self.count];
    [self.roundViewArray.firstObject removeFromSuperview];
    [self.roundViewArray removeObjectAtIndex:0];
}       

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容