iphone用Animation实现动画效果

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

主要代码如下:
- (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:是动画完成以后所要执行的代码块儿。
如需要源码,源码下载地址
http://download.csdn.net/detail/rhljiayou/4922322
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值