IOS動畫與繪圖
目錄:UIView動畫 子視圖翻轉動畫UIImageView動畫CATransition動畫Core Graphics繪圖:(線、矩形、曲線、文字、圖片)CALayer 核心動畫:(主要畫面格動畫、 單一動畫、組動畫) 1.UIView動畫(1)設定代理(viewController)動畫開始: beginAnimations設定動畫加減速方式: setAnimationCurve設定持續時間長度: setAnimationDuration設定動畫重複次數: setAnimationRepeatCount設定動畫是否做反向操作: setAnimationRepeatAutoreverses:YES設定視圖的變化,提交動畫 :[UIView commitAnimations]; (2)Block動畫eg:[UIView animateWithDuration:2 animations:^{ baseView.alpha = 0; } completion:^(BOOL finished) { NSLog(@"end"); }]; 2.UIImage動畫①建立 UIImageView對象②建立一個數組,存放多個UIImage對象③為UIImageView對象設定動畫數組:imgView.animationImages=mArr;④設定時間長度,開始動畫:startAnimating 3.CATransition動畫①建立 CATransition對象②設定對象屬性(時間長度Duration、速率TimingFunction、類型type、方向subType)類型有私人API:// animation.type = @"cube" //立方體效果// animation.type = @"suckEffect" //收縮效果,如一塊布被抽走// animation.type = @"oglFlip" //翻轉效果// animation.type = @"rippleEffect" //滴水效果// animation.type = @"pageCurl" //向上翻一頁// animation.type = @"pageUnCurl" //向下翻一頁 ③為UIView對象添加動畫,添加到它的Layer上為導覽列添加動畫(立方體旋轉效果)[self.navigationController.view.layer addAnimation:transition forKey:@"animation"]; 4.繪圖調用父類方法,程式運行時自動調用- (void)drawRect:(CGRect)rect{ [super drawRect:rect];}繪畫不同圖形,都需要先擷取畫布//映像上下文,擷取畫布,他是一個結構體指標 CGContextRef context = UIGraphicsGetCurrentContext();都可以設定畫筆屬性://設定虛線 CGFloat arr[] = {10,5}; CGContextSetLineDash(context, 0, arr, 2); //設定畫筆的寬度 CGContextSetLineWidth(context, 2); //設定畫筆顏色 CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); //設定填充色 CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);最後都要儲存,添加到畫布 //儲存路徑,添加繪圖 CGContextDrawPath(context, kCGPathEOFillStroke);(1)畫直線//擷取畫筆,設定起點 CGContextMoveToPoint(context, 20, 20); //移動畫筆(可多次移動,串連成不同圖形) CGContextAddLineToPoint(context, 220, 20);(2)畫矩形//繪製矩形 CGContextAddRect(context, CGRectMake(20, 20, 100, 100));(3)畫曲線// //繪製貝茲路徑1// CGContextMoveToPoint(context, 10, 100);// CGContextAddCurveToPoint(context, 55, 0, 145, 200, 200, 100);// //繪製貝茲路徑2// CGContextMoveToPoint(context, 10, 200);// CGContextAddQuadCurveToPoint(context, 105, 0,200, 200) ; //繪製扇形 CGContextAddArc(context, 100, 100, 100, arc(90), arc(0), 1); (4)畫映像(Image)可使用PrintCode軟體畫,自動產生代碼,粘貼過去 5.繪製陰影建立UIView對象設定圓角(必須是layer層)//// view.layer.cornerRadius = 10;//// view.layer.masksToBounds = YES;***設定圓角時不會顯示陰影可以設定邊框,陰影顏色( view.layer.shadowColor)、陰影透明度( view.layer.shadowOpacity)、圖片( view.layer.contents)6.主要畫面格動畫建立路徑,在路徑上添加運動軌跡//路徑 CGMutablePathRef path = CGPathCreateMutable(); //為路徑添加一個運動軌跡 CGPathAddRect(path, nil, CGRectMake(10, 10, 200, 200));建立主要畫面格動畫 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];設定動畫的加減速方式、時間長度、重複次數、路徑( animation.path = path;)建立單一動畫 CABasicAnimation對象,旋轉basicAnimation.fromValue = @0; basicAnimation.toValue = @(2 * M_PI); 設定組動畫,可以將上面建立的主要畫面格動畫和單一動畫添加進去,如下,實現兩種動畫同時進行//組動畫 CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; //往組裡添加兩個動畫 animationGroup.animations = @[animation,basicAnimation]; animationGroup.duration = 2; animationGroup.repeatCount = 1000; [lView.layer addAnimation:animationGroup forKey:@"animationGroup"];