Xcode学习之视图转换例子实践

移动开发 iOS
本文介绍的是Xcode学习之视图转换例子实践,讲述了xcode中视图转换例子实践的内容,先来看详细内容。

Xcode学习之视图转换例子实践是本文要介绍的内容,主要介绍了xcode视图转换例子实践的教程。让我们进一步的去学习xcode的相关内容,先来看本文详细介绍。

翻转(类似翻书)视图效果,两种实现方式

滑动视图效果

分别看各自实现的重点:

翻转视图效果例子

在官方上,提供

  1. UIViewAnimationTransitionFlipFromLeft和UIViewAnimationTransitionFlipFromRight 

方法,来实现视图向左或向右翻转。

在UIView动画块中使用转换,需要2个工作:

1、必须将转换作为块参数添加

2、应该在块内部重新安排视图顺序。

效果代码如下:

  1. - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{      
  2. // Start Animation Block     
  3.  CGContextRef context = UIGraphicsGetCurrentContext();      
  4.  [UIView beginAnimations:nil context:context];      
  5.  [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:[self superview] cache:YES];  
  6.  //*    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];      
  7.  [UIView setAnimationDuration:1.0];         
  8.  // Animations    [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  9.  //*         
  10.  // Commit Animation Block      
  11.  [UIView commitAnimations];  
  12.  } 

注意,此代码写在touchesEnded事件上的,也是符合翻转逻辑

上述代码中带有//*的地方,就是所需2个工作。

***处表示向左翻转,翻转的对象是当前视图的上级视图,并缓存

第二处表示子视图集合中,0和1之间交换

UIView类

类方法:(动画部分)

  1. setAnimationTransition:forView:cache:  
  2. + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache  
  3.  
  4. Sets a transition to apply to a view during an animation block. 

方法:

  1. exchangeSubviewAtIndex:withSubviewAtIndex:  
  2.     - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2  
  3.  
  4.     Exchanges the subviews at the specified indices.  
  5.     index1: The index of the first subview in the receiver.  
  6.     index2: The index of the second subview in the receiver. 

关于方法exchangeSubviewAtIndex:withSubviewAtIndex:实现的效果也可以用其他方式来实现。比如:

  1. UIViewController Controller  
  2.     UIView v1  
  3.     UIView v2  
  4. Controller.view = v1;//v1 front  
  5. Controller.view = v2;//v2 front 

当然,这只是实践中应用,但不一定这么用。用UIViewController实现不了动画效果,至少现在我不知道UIViewController本身可否实现动画效果。

关于另外一种方式来实现动画效果Core Animation Transition,作用于层,而非视图,看如下代码:

  1. - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{      
  2. CATransition *animation = [CATransition animation];      
  3. [animation setDelegate:self];      
  4. [animation setDuration:1.0f];      
  5. [animation setTimingFunction:UIViewAnimationCurveEaseInOut];     
  6. [animation setType: kCATransitionPush];      
  7. [animation setSubtype: kCATransitionFromLeft];         
  8. [[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];      
  9. [[[self superview] layer] addAnimation:animation forKey:@"transitionViewAnimation"];    
  10.  } 

#p#

CATransition类此类针对层执行转换动画继承CAAnimation : NSObject属性:

  1. delegate:  
  2. @property(retain) id delegate  
  3.  
  4.     Specifies the receiver’s delegate object.  
  5.  
  6. duration:  
  7. @property CFTimeInterval duration  
  8.  
  9.     Specifies the basic duration of the animation, in seconds. (required)  
  10.  
  11. timingFunction:  
  12. @property(retain) CAMediaTimingFunction *timingFunction  
  13.  
  14.     An optional timing function defining the pacing of the animation.  
  15.  
  16. subtype  
  17. @property(copy) NSString *subtype  
  18.     Specifies an optional subtype that indicates the direction for the predefined motion-based transitions.  
  19.  
  20. Discussion  
  21.     The possible values are shown in “Common Transition Subtypes”. The default is nil.  
  22.  
  23. type  
  24. @property(copy) NSString *type  
  25. Discussion  
  26.     The possible values are shown in “Common Transition Types”. The default is kCATransitionFade. 

Constants/常量

  1. Common Transition Types  
  2.     These constants specify the transition types that can be used with the type property.  
  3.     NSString * const kCATransitionFade;  
  4.     NSString * const kCATransitionMoveIn;  
  5.     NSString * const kCATransitionPush;  
  6.     NSString * const kCATransitionReveal;  
  7.     kCATransitionFade  
  8.         The layer’s content fades as it becomes visible or hidden.  
  9.     kCATransitionMoveIn  
  10.         The layer’s content slides into place over any existing content. The “Common Transition Subtypes” are used with this transition.  
  11.     kCATransitionPush  
  12.         The layer’s content pushes any existing content as it slides into place. The “Common Transition Subtypes” are used with this transition.  
  13.     kCATransitionReveal  
  14.         The layer’s content is revealed gradually in the direction specified by the transition subtype. 
  15. The “Common Transition Subtypes” are used with this transition.  
  16. Common Transition Subtypes  
  17.     These constants specify the direction of motion-based transitions. They are used with the subtype property.  
  18.     NSString * const kCATransitionFromRight;  
  19.     NSString * const kCATransitionFromLeft;  
  20.     NSString * const kCATransitionFromTop;  
  21.     NSString * const kCATransitionFromBottom;  
  22.     kCATransitionFromRight  
  23.         The transition begins at the right side of the layer.  
  24.     kCATransitionFromLeft  
  25.         The transition begins at the left side of the layer.  
  26.     kCATransitionFromTop  
  27.         The transition begins at the top of the layer.  
  28.     kCATransitionFromBottom  
  29.         The transition begins at the bottom of the layer.  
  30.     Declared in CAAnimation.h. 

在后续例子中也有此CATransition类的学习,具体方法实际中去参考CALayer类。

方法:

  1. addAnimation:forKey:  
  2.     - (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key  
  3.     Add an animation object to the receiver’s render tree for the specified key.  
  4.     anim: The animation to be added to the render tree.  
  5.     key: A string that specifies an identifier for the animation. 

在后续的滑动视图中,使用CATransition实现,关键在于生成一个控制层运动的对象,看代码:

  1. - (CATransition *) getAnimation:(NSString *) direction{     
  2.  CATransition *animation = [CATransition animation];      
  3.  [animation setDelegate:self];     
  4.   [animation setType:kCATransitionPush];     
  5.    [animation setSubtype:direction];      
  6.    [animation setDuration:1.0f];      
  7.    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];  
  8. return animation;  

  1. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

这句代码和前面有些不一样吧。

另外一个关键:定义一个滑动方向,在touchesBegan初始化,在touchesMoved获取当前值,在touchesEnded中使用。多阅读此代码

小结:Xcode学习之视图转换例子实践的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-01 10:01:12

Xcode UIView 动画

2011-07-20 14:31:56

XCode User Scrip 脚本

2011-08-01 15:57:58

2011-08-01 16:50:28

Xcode 动态 View

2011-08-01 17:01:02

Xcode WindowBase View

2011-08-01 17:50:28

Xcode

2011-08-10 14:00:22

XcodeUIWebView视频

2011-08-11 16:31:08

XCode

2011-08-18 10:17:21

Xcode4Xcode

2015-05-25 10:01:17

WatchKitAPP

2011-07-25 15:42:38

Xcode Vim

2010-04-19 10:20:19

Oracle参数

2011-08-08 17:05:02

XCode UserScript 脚本

2011-08-01 09:26:51

Xcode Xcode 4 Instrument

2011-08-19 15:16:41

XCodeUserScripts脚本

2013-07-25 15:19:23

iOS开发学习Xcode打包framiOS开发

2011-08-01 17:31:25

Xcode开发 Cocoa

2011-07-29 18:52:59

Xcode安装 MacOS Windows

2014-03-12 09:52:17

XcodeCode Snippe

2011-07-19 15:55:09

Xcode Interface Builder
点赞
收藏

51CTO技术栈公众号