如何用Animation实现iphone动画效果

悟途网 2013年06月06日 23:08 阅读()
字号 (A- A+)

1、写了一个小Demo,实现上下移动的效果图:

Animation实现iphone动画效果

Animation实现iphone动画效果

Animation实现iphone动画效果

主要代码如下:

- (IBAction)upChange:(id)sender { if (typeView == down) { [self.downBtn setHidden:NO]; [self.upBtn setHidden:NO]; [UIView animateWithDuration:0.5 animations:^{ self.upView.center = CGPointMake(self.upView.center.x, 0); self.downView.center = CGPointMake(self.downView.center.x, (self.upView.frame.size.height+self.downView.frame.size.height)/2); }]; typeView = middle; }else if(typeView == middle){ [self.downBtn setHidden:NO]; [self.upBtn setHidden:YES]; [UIView animateWithDuration:0.5 animations:^{ self.upView.center = CGPointMake(self.upView.center.x, -(self.upView.frame.size.height/2)); self.downView.center = CGPointMake(self.downView.center.x, self.downView.frame.size.height/2); }]; typeView = up; } } - (IBAction)downChange:(id)sender { if (typeView == middle) { [self.downBtn setHidden:YES]; [self.upBtn setHidden:NO]; [UIView animateWithDuration:0.5 animations:^{ self.upView.center = CGPointMake(self.upView.center.x, self.upView.frame.size.height/2); self.downView.center = CGPointMake(self.downView.center.x, self.upView.frame.size.height+self.downView.frame.size.height/2); }]; typeView = down; }else if(typeView == up) { [self.downBtn setHidden:NO]; [self.upBtn setHidden:NO]; [UIView animateWithDuration:0.5 animations:^{ self.upView.center = CGPointMake(self.upView.center.x, 0); self.downView.center = CGPointMake(self.downView.center.x, (self.upView.frame.size.height+self.downView.frame.size.height)/2); }]; typeView = middle; } }

这是主要的代码,另外还有一些需要注意的一些小细,如你的两个view的大小,需要计算出精确位置。

下面开始学习[UIView animateWithDuration]方法的使用:

函数原型:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL
duration:动画持续时间
animations:该代码儿块中是所有view要完成的动画的所有属性的定义;
还有另外两个函数:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);// delay = 0.0, options = 0

options:是动画执行的选项

completion:是动画完成以后所要执行的代码块儿。

浏览过的人还看过的文章

热门文章
随机推荐
苹果iPhone手机手电筒亮度调节教程

苹果iPhone手机手电筒亮度调

苹果在最新的iOS10系统中带来了不少改进,其中iPhone6...

iPhone6s有必要升级iOS11系统吗?iOS11好用吗?

iPhone6s有必要升级iOS11系统吗

iPhone6s升级iOS10.3.2好用吗?要不要升级iOS11呢?想必有很...

iOS 9越狱后必备插件汇总

iOS 9越狱后必备插件汇总

iOS9必备插件有哪些?盘古大神iOS9完美越狱出来啦,越...

防止孩子删除iPhone手机App应用的方法

防止孩子删除iPhone手机App应

随着iPhone的普及,经常可以看到玩iPhone手机的孩子。在...

怎么在ios代码控制出现控件的阴影

怎么在ios代码控制出现控件

怎么在ios代码控制出现控件的阴影,只需要把对应的空...

如何更快更好用iPhone6加速优化方法

如何更快更好用iPhone6加速优

怎样在不影响主体功能的前提下尽量提高iPhone6的性能呢...

iPhone手机怎么设置一个回电提醒的方法

iPhone手机怎么设置一个回电

来电并不总是在适当的时候出现,如果你的iPhone在开会...

iPhone小圆点AssistiveTouch使用大全

iPhone小圆点AssistiveTouch使用

小圆点AssistiveTouch并不是随iPhone出生一起诞生的,直到...