IOS开发

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

2016-08-17  本文已影响0人  WTFSSD
-(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];
}       

上一篇 下一篇

猜你喜欢

热点阅读