2016-11-19 186 views
1

我有一個UIView,我想要在類似於下載飛行到Safari停靠欄的曲線路徑上設置動畫。我不知道該怎麼做,因爲我確定它涉及到Core Graphics和Core Animation。我發現的最後一個答案是從2011年開始使用iOS 6中棄用的函數,但無法按原樣運行。提前致謝。路徑動畫曲線核心圖形

回答

0

讓我們的曲線點P1,P2, p3,p4和p5,&找到每對相鄰點的中點。將m1標記爲p1和p2的中點。類似地,m2,m3,m4。

  • 將四邊形曲線添加到以p2作爲控制點的m2點。
  • 將p3的四邊形曲線添加到點m3作爲控制點。
  • 以p4作爲控制點,將四條曲線添加到點m4。

代碼:

CGFloat screenWidth = self.view.frame.size.width; 
CGFloat screenHeight = self.view.frame.size.height; 

UIView *aniView = [[UIView alloc] initWithFrame:CGRectMake(50, screenHeight, 50, 50)]; 
[aniView setBackgroundColor:[UIColor redColor]]; 
aniView.layer.cornerRadius = 25.0; 
[self.view addSubview:aniView]; 


UIBezierPath *movePath = [UIBezierPath bezierPath]; 
[movePath moveToPoint:aniView.center]; 
[movePath addQuadCurveToPoint:CGPointMake(screenWidth-50,screenHeight-50) 
       controlPoint:CGPointMake(screenWidth/2,screenHeight-150)]; 
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
moveAnim.path = movePath.CGPath; 
moveAnim.removedOnCompletion = YES; 
CAAnimationGroup *animGroup = [CAAnimationGroup animation]; 
animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil]; 
animGroup.duration = 2.0; 

[CATransaction begin]; { 
    [CATransaction setCompletionBlock:^{ 
     [aniView.layer removeAllAnimations]; 
     [aniView removeFromSuperview]; 
    }]; 

    [aniView.layer addAnimation:animGroup forKey:nil]; 

} [CATransaction commit]; 

複製過去上面的代碼到一些方法,並嘗試調用方法...