Core Anitmation 是什麼??框架
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; [self moveToPoint:point]; } -(void)moveToPoint:(CGPoint)point{ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"] ; // 1.設置目的點 animation.toValue =[NSValue valueWithCGPoint:point]; // 2.設置動畫時長 animation.duration =0.5; // 3.設置完成後刪除動畫 animation.removedOnCompletion = NO; // 4.設置模式爲向前填充 animation.fillMode = kCAFillModeBoth; // 5.設置速度模式爲先慢後快 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; // 6.設置開始時長延後1秒 animation.beginTime =CACurrentMediaTime() + 1; // 7.將動畫添加到Layer上開始執行 [_redview.layer addAnimation:animation forKey:nil]; }
將上述代碼插入到一視圖控制器中,點屏幕上點擊一下看看效果~函數
下面看一下fillMode是幹啥的動畫
#pragma mark 暫停動畫 - (void)pause { // 記錄中止瞬間的時間偏移量,取出當前動畫定格對應的事件 CFTimeInterval time = [_redView.layer convertTime:CACurrentMediaTime() fromLayer:nil]; // 利用圖層的timeOffset記錄暫停的時間偏移量 _redView.layer.timeOffset = time; NSLog(@"%f", time); // 將速度設置爲0,能夠中止動畫 _redView.layer.speed = 0.0; } #pragma mark 繼續動畫 - (void)resume { // 恢復時間量 CFTimeInterval pauseTime = _redView.layer.timeOffset; // 取當前媒體時間 CFTimeInterval time = CACurrentMediaTime(); // 計算時間差值 CFTimeInterval offset = time - pauseTime; NSLog(@"%f", offset); // 設置圖層動畫的起始時間 _redView.layer.beginTime = offset; _redView.layer.timeOffset = 0; // 恢復速度爲1 _redView.layer.speed = 1.0; } // 用戶擡起手指,纔會執行,一般會針對touchesMove中的處理,作一些收尾工做 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } #pragma mark - 動畫代理方法 - (void)animationDidStart:(CAAnimation *)anim { NSLog(@"動畫開始 %@", NSStringFromCGPoint(_redView.center)); } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { CABasicAnimation *animation = (CABasicAnimation *)anim; // 動畫結束後,修正視圖的位置 CGPoint point = [animation.toValue CGPointValue]; _redView.center = point; NSLog(@"動畫結束 %@ %@", NSStringFromCGPoint(_redView.center), animation.toValue); } #pragma mark - 動畫方法 #pragma mark 旋轉動畫 - (void)rotation { CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; _redView.layer.anchorPoint = CGPointMake(1, 1); // 設置旋轉角度,轉360 anim.toValue = @(2 * M_PI); // 要無限循環轉動,指定一個很是大的數值 // 對於無限循環的動畫,須要處理動畫累加的問題 anim.repeatCount = HUGE_VALF; // anim.repeatCount = MAXFLOAT; // 若是要退出到後臺,再次回覆的時候繼續執行動畫,須要設置removedOnCompletion = NO anim.removedOnCompletion = NO; anim.duration = 1.0f; [_redView.layer addAnimation:anim forKey:@"myRotation"]; } #pragma mark 縮放動畫 - (void)scale { // 1. 實例化 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; // 2. toValue anim.toValue = @(2.0); anim.repeatCount = 3; // 自動反向執行動畫 anim.autoreverses = YES; anim.duration = 0.3f; [_redView.layer addAnimation:anim forKey:nil]; }