UIView的Animaltion

Object-c代码

@interface UIView(UIViewAnimation)+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested+ (void)commitAnimations; // starts up any animations when the top level animation is commited// no getters. if called outside animation block, these setters have no effect.+ (void)setAnimationDelegate:(id)delegate; // default = nil+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations — allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // current limitation – only one per begin/commit block+ (void)setAnimationsEnabled:(BOOL)enabled; // ignore any attribute changes while set.+ (BOOL)areAnimationsEnabled;@end

复制代码

这些方法非常的不直观,开发者还是需要花很多时间去思考怎么组合这些方法。但是自从iOS4.0提供块语法支持之后,苹果公司把动画效果的实现封装了一下,效果立杆见影,直观了许多,因此大家完全可以不用去看上面的那些方法,重点关注如下的方法:Object-c代码

@interface UIView(UIViewAnimationWithBlocks)+ (void)animateWithDuration:(NSTimeInterval)durationdelay:(NSTimeInterval)delayoptions:(UIViewAnimationOptions)optionsanimations:(void (^)(void))animationscompletion:(void (^)(BOOL finished))completion;+ (void)animateWithDuration:(NSTimeInterval)durationanimations:(void (^)(void))animationscompletion:(void (^)(BOOL finished))completionNS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0+ (void)animateWithDuration:(NSTimeInterval)durationanimations:(void (^)(void))animationsNS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL+ (void)transitionWithView:(UIView *)viewduration:(NSTimeIntervl)durationoptions:(UIViewAnimationOptins)optionsanimations:(void (^)(void)animationscompletion:(void (^)(BOOL finished) completionNS_AVAILABLE_IOS(4_0);+ (void)transitionFromView:(UIView *)fromViewtoView:(UIView *)toViewduration:(NSTimeInterval)durationoptions:(UIViewAnimationOptions)optionscompletion:(void (^)(BOOL finished))completionNS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview@end

复制代码

上面的几个方法从名字上看就非常直观。前三个方法都可以按如下的方式直译,只是后两个使用了一些默认参数而已:Java代码

做一个动画效果,持续时间为duration,延迟delay秒开始执行 ,以options指定的方式运行这个动画,animations块中指定哪些UIView会参加本次动画效果,以及动画效果完成时这些UIView会是一个什么状态,动画完成之后,执行completion块进行收尾。

复制代码

有了这3个方法,开发者只需要思考,初始值,结果值,持续时间,运行方式就行了,具体的细节移动都交给类库。后2个方法是用于UIView相互之间转换的,个人觉得用处不大,因为用上面的三个方法同样可以做到这些效果,因此略过。关于UIView的动画效果支持,有2点值得一提* 上面所有的方法都是类方法,当调用这些方法之后,系统会新起线程执行动画效果,不会阻塞主线程的执行。* UIView的Animation效果只支持一些简单的2D动画效果,复杂的大家还得研究Core Animation。一个实战例子在我写的一个小游戏的主机界面中,我使用了一点动画的效果,,主界面的设计图如下:

动画后的效果图如下:

我想要的效果就是,加载主界面后,图片缓缓的展开成扇形,然后游戏的菜单显示供玩家点击。代码如下:首先,准备动画前状态,让想展示的UIView不可见:Object-c代码

什么天荒地老,什么至死不渝。都只是锦上添花的借口…

UIView的Animaltion

相关文章:

你感兴趣的文章:

标签云: