Xcode學習之視圖轉(zhuǎn)換例子實踐
Xcode學習之視圖轉(zhuǎn)換例子實踐是本文要介紹的內(nèi)容,主要介紹了xcode中視圖轉(zhuǎn)換例子實踐的教程。讓我們進一步的去學習xcode的相關(guān)內(nèi)容,先來看本文詳細介紹。
翻轉(zhuǎn)(類似翻書)視圖效果,兩種實現(xiàn)方式
滑動視圖效果
分別看各自實現(xiàn)的重點:
翻轉(zhuǎn)視圖效果例子
在官方上,提供
- UIViewAnimationTransitionFlipFromLeft和UIViewAnimationTransitionFlipFromRight
方法,來實現(xiàn)視圖向左或向右翻轉(zhuǎn)。
在UIView動畫塊中使用轉(zhuǎn)換,需要2個工作:
1、必須將轉(zhuǎn)換作為塊參數(shù)添加
2、應(yīng)該在塊內(nèi)部重新安排視圖順序。
效果代碼如下:
- - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
- // Start Animation Block
- CGContextRef context = UIGraphicsGetCurrentContext();
- [UIView beginAnimations:nil context:context];
- [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];
- //* [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:1.0];
- // Animations [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
- //*
- // Commit Animation Block
- [UIView commitAnimations];
- }
注意,此代碼寫在touchesEnded事件上的,也是符合翻轉(zhuǎn)邏輯
上述代碼中帶有//*的地方,就是所需2個工作。
***處表示向左翻轉(zhuǎn),翻轉(zhuǎn)的對象是當前視圖的上級視圖,并緩存
第二處表示子視圖集合中,0和1之間交換
UIView類
類方法:(動畫部分)
- setAnimationTransition:forView:cache:
- + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
- Sets a transition to apply to a view during an animation block.
方法:
- exchangeSubviewAtIndex:withSubviewAtIndex:
- - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2
- Exchanges the subviews at the specified indices.
- index1: The index of the first subview in the receiver.
- index2: The index of the second subview in the receiver.
關(guān)于方法exchangeSubviewAtIndex:withSubviewAtIndex:實現(xiàn)的效果也可以用其他方式來實現(xiàn)。比如:
- UIViewController Controller
- UIView v1
- UIView v2
- Controller.view = v1;//v1 front
- Controller.view = v2;//v2 front
當然,這只是實踐中應(yīng)用,但不一定這么用。用UIViewController實現(xiàn)不了動畫效果,至少現(xiàn)在我不知道UIViewController本身可否實現(xiàn)動畫效果。
關(guān)于另外一種方式來實現(xiàn)動畫效果Core Animation Transition,作用于層,而非視圖,看如下代碼:
- - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
- CATransition *animation = [CATransition animation];
- [animation setDelegate:self];
- [animation setDuration:1.0f];
- [animation setTimingFunction:UIViewAnimationCurveEaseInOut];
- [animation setType: kCATransitionPush];
- [animation setSubtype: kCATransitionFromLeft];
- [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
- [[[self superview] layer] addAnimation:animation forKey:@"transitionViewAnimation"];
- }
#p#
CATransition類此類針對層執(zhí)行轉(zhuǎn)換動畫繼承CAAnimation : NSObject屬性:
- delegate:
- @property(retain) id delegate
- Specifies the receiver’s delegate object.
- duration:
- @property CFTimeInterval duration
- Specifies the basic duration of the animation, in seconds. (required)
- timingFunction:
- @property(retain) CAMediaTimingFunction *timingFunction
- An optional timing function defining the pacing of the animation.
- subtype
- @property(copy) NSString *subtype
- Specifies an optional subtype that indicates the direction for the predefined motion-based transitions.
- Discussion
- The possible values are shown in “Common Transition Subtypes”. The default is nil.
- type
- @property(copy) NSString *type
- Discussion
- The possible values are shown in “Common Transition Types”. The default is kCATransitionFade.
Constants/常量
- Common Transition Types
- These constants specify the transition types that can be used with the type property.
- NSString * const kCATransitionFade;
- NSString * const kCATransitionMoveIn;
- NSString * const kCATransitionPush;
- NSString * const kCATransitionReveal;
- kCATransitionFade
- The layer’s content fades as it becomes visible or hidden.
- kCATransitionMoveIn
- The layer’s content slides into place over any existing content. The “Common Transition Subtypes” are used with this transition.
- kCATransitionPush
- The layer’s content pushes any existing content as it slides into place. The “Common Transition Subtypes” are used with this transition.
- kCATransitionReveal
- The layer’s content is revealed gradually in the direction specified by the transition subtype.
- The “Common Transition Subtypes” are used with this transition.
- Common Transition Subtypes
- These constants specify the direction of motion-based transitions. They are used with the subtype property.
- NSString * const kCATransitionFromRight;
- NSString * const kCATransitionFromLeft;
- NSString * const kCATransitionFromTop;
- NSString * const kCATransitionFromBottom;
- kCATransitionFromRight
- The transition begins at the right side of the layer.
- kCATransitionFromLeft
- The transition begins at the left side of the layer.
- kCATransitionFromTop
- The transition begins at the top of the layer.
- kCATransitionFromBottom
- The transition begins at the bottom of the layer.
- Declared in CAAnimation.h.
在后續(xù)例子中也有此CATransition類的學習,具體方法實際中去參考CALayer類。
方法:
- addAnimation:forKey:
- - (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key
- Add an animation object to the receiver’s render tree for the specified key.
- anim: The animation to be added to the render tree.
- key: A string that specifies an identifier for the animation.
在后續(xù)的滑動視圖中,使用CATransition實現(xiàn),關(guān)鍵在于生成一個控制層運動的對象,看代碼:
- - (CATransition *) getAnimation:(NSString *) direction{
- CATransition *animation = [CATransition animation];
- [animation setDelegate:self];
- [animation setType:kCATransitionPush];
- [animation setSubtype:direction];
- [animation setDuration:1.0f];
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- return animation;
- }
看
- [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
這句代碼和前面有些不一樣吧。
另外一個關(guān)鍵:定義一個滑動方向,在touchesBegan初始化,在touchesMoved獲取當前值,在touchesEnded中使用。多閱讀此代碼
小結(jié):Xcode學習之視圖轉(zhuǎn)換例子實踐的內(nèi)容介紹完了,希望本文對你有所幫助!