IOS UIVIEW layer動畫 總結
阿新 • • 發佈:2019-01-24
//翻頁效果動畫 左邊 [UIView beginAnimations:@"animation" context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:YES]; [UIView commitAnimations]; //翻頁效果動畫 右邊 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; [UIView setAnimationDuration:0.35f]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO]; [UIView commitAnimations]; //lar動畫,從上到下模糊 CATransition *animation = [CATransition animation]; [animation setDuration:2.0f]; [animation setFillMode:kCAFillModeForwards]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; [animation setType:kCATransitionPush]; [animation setSubtype:kCATransitionFromBottom]; [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil]; //摺頁效果動畫 [UIView animateWithDuration:0.35f animations:^ { /** * @see http://donbe.blog.163.com/blog/static/138048021201061054243442/ * * @param transform 形變屬性(結構體),可以利用這個屬性去對view做一些翻轉或者縮放.詳解請猛戳↑URL. * * @method valueWithCATransform3D: 此方法需要一個CATransform3D的結構體.一些非詳細的講解可以看下面的URL * * @see http://blog.csdn.net/liubo0_0/article/details/7452166 * */ self.navigationController.view.transform = CGAffineTransformMakeScale(0.001, 0.001); CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; // 向右旋轉45°縮小到最小,然後再從小到大推出. animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)]; /** * 其他效果: * 從底部向上收縮一半後彈出 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)]; * * 從底部向上完全收縮後彈出 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)]; * * 左旋轉45°縮小到最小,然後再從小到大推出. * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)]; * * 旋轉180°縮小到最小,然後再從小到大推出. * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)]; */ animation.duration = 2; animation.repeatCount = 1; [self.navigationController.view.layer addAnimation:animation forKey:nil]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.35f animations:^ { self.navigationController.view.transform = CGAffineTransformMakeScale(1.0, 1.0); }]; }]; //從下到上模糊推出 CATransition *animation = [CATransition animation]; [animation setDuration:1.0f]; [animation setType:kCATransitionReveal]; [animation setSubtype:kCATransitionFromTop]; [animation setFillMode:kCAFillModeForwards]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil]; //旋轉動畫 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2]; rotationAnimation.duration = 0.35f; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.toValue = [NSNumber numberWithFloat:0.0]; scaleAnimation.duration = 0.35f; scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; animationGroup.duration = 2.35f; animationGroup.autoreverses = YES; animationGroup.repeatCount = 1; animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, nil]; [self.navigationController.view.layer addAnimation:animationGroup forKey:@"animationGroup"]; [UIView commitAnimations]; CATransition *animation = [CATransition animation]; [animation setDuration:0.8]; /* 各種動畫效果*/ /* type型別 */ /*這裡要注意不用私有方法*/ /* pageCurl 向上翻一頁 pageUnCurl 向下翻一頁 rippleEffect 滴水效果 suckEffect 收縮效果,如一塊布被抽走 cube 立方體效果 oglFlip 上下翻轉效果 */ [animation setType: kCATransitionReveal]; /* 動畫方向*/ /* SubType型別 */ /* kCATransitionFade淡出 kCATransitionMoveIn覆蓋原圖 kCATransitionPush推出 kCATransitionReveal底部顯出來 也可以有四種類型: kCATransitionFromRight; kCATransitionFromLeft kCATransitionFromTop; kCATransitionFromBottom */ [animation setSubtype: kCATransitionFromBottom]; /* 動畫的開始與結束的快慢*/ [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [self.navigationController.self.view.layer addAnimation:animation forKey:nil]; //這裡應用場景是已經有2個viewController插入要的self.view上 /* [self.view insertSubview:self.blueController.view atIndex:0]; [self.view insertSubview:self.yellowController.view atIndex:1]; */ [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; //UIView開始動畫,第一個引數是動畫的標識,第二個引數附加的應用程式資訊用來傳遞給動畫代理訊息 [UIView beginAnimations:@"animation" context:nil]; //動畫持續時間 [UIView setAnimationDuration:1.25]; //設定動畫的回撥函式,設定後可以使用回撥方法 [UIView setAnimationDelegate:self]; //設定動畫曲線,控制動畫速度 [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; //設定動畫方式,並指出動畫發生物件 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:YES];//cache /*cache 如果是YES,那麼在開始和結束圖片檢視渲染一次並在動畫中建立幀;否則,檢視將會在每一幀都渲染。例如快取,你不需要在檢視轉變中不停的更新,你只需要等到轉換完成再去更新檢視。 討論 */ //設定動畫重複 [UIView setAnimationRepeatCount:5.0]; //提交UIView動畫 結束動畫 [UIView commitAnimations]; //開始一個動畫塊 [UIView beginAnimations:@"animationID" context:nil]; //設定動畫塊中的動畫持續時間(用秒) [UIView setAnimationDuration:1.5f]; //設定動畫塊中的動畫屬性變化的曲線 [UIView setAnimationCurve:UIViewAnimationCurveLinear]; /*變化曲線還有 ( UIViewAnimationCurveEaseInOut, // slow at beginning and end UIViewAnimationCurveEaseIn, // slow at beginning UIViewAnimationCurveEaseOut, // slow at end UIViewAnimationCurveLinear) */ //設定動畫塊中的動畫效果是否自動重複播放。 [UIView setAnimationRepeatAutoreverses:NO]; //設定動畫在動畫模組中的重複次數 //setAnimationRepeatCount: //設定動畫訊息的代理。 [UIView setAnimationDelegate:self]; /* UIView動畫的代理方法 // [UIView // //設定訊息給動畫代理當動畫開始的時候 // setAnimationWillStartSelector:@selector(resizeAnimationWillStart:context:)]; // [UIView // //設定訊息給動畫代理當動畫停止的時候 // setAnimationDidStopSelector:@selector(resizeAnimationDidStop:finished:context:)]; //將效果作用在指定的view */ [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:YES]; /* 效果還有(UIViewAnimationTransitionFlipFromLeft,UIViewAnimationTransitionFlipFromRight,UIViewAnimationTransitionCurlUp,UIViewAnimationTransitionCurlDown) */ //顯示在最前面 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; //結束動畫 [UIView commitAnimations];