[摘要]本文是對ios按鈕點擊後翻轉效果的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。
代碼是網上找到的,不過找到的時候直接復制下來不能用,稍微整理下,為和我一樣水平的菜鳥觀摩一下下。
(1)引入“QuartzCore.framework”庫,頭部引用。
C代碼
- #include<QuartzCore/CoreAnimation.h>
(2)直接上代碼,你懂的。
C代碼
- -(IBAction)buttonP:(id)sender{
- [selfbuttonAnimation:sender];
- }
- -(CAAnimation*)animationRotate{
- CATransform3DrotationTransform=CATransform3DMakeRotation(M_PI/2,0,1,0);
- CABasicAnimation*animation;
- animation=[CABasicAnimationanimationWithKeyPath:@"transform"];
- animation.toValue=[NSValuevalueWithCATransform3D:rotationTransform];
- animation.duration=3;
- animation.autoreverses=YES;
- animation.cumulative=YES;
- animation.repeatCount=1;
- animation.beginTime=0.1;
- animation.delegate=self;
- returnanimation;
- }
- -(void)buttonAnimation:(id)sender{
- UIButton*theButton=sender;
- CAAnimation*myAnimationRotate=[selfanimationRotate];
- CAAnimationGroup*m_pGroupAnimation;
- m_pGroupAnimation=[CAAnimationGroupanimation];
- m_pGroupAnimation.delegate=self;
- m_pGroupAnimation.removedOnCompletion=NO;
- m_pGroupAnimation.duration=10;
- m_pGroupAnimation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- m_pGroupAnimation.repeatCount=1;
- m_pGroupAnimation.fillMode=kCAFillModeForwards;
- m_pGroupAnimation.animations=[NSArrayarrayWithObjects:myAnimationRotate,nil];
- [theButton.layeraddAnimation:m_pGroupAnimationforKey:@"animationRotate"];
- }
- -(void)animationDidStop:(CAAnimation*)animfinished:(BOOL)flag{
- //todo
- }
PS:
CATransform3DMakeRotation(M_PI/2,0,1,0);第一個參數是旋轉的角度,有一點需要著名,就是對象回按照你設定的角度的最短距離去旋轉,後面三個參數分別是xyz(-1~1之間的值)代表的一個向量值。順時針或者逆時針旋轉尚未搞定具體是什麼參數來控制的,有知道的朋友提醒下,謝謝!