IT虾米网

iOS 移动动画简单实现

shasha 2018年06月23日 手机开发 1120 0
[UIView animateWithDuration:0.5 animations:^{ 
            for (i = num; i > _indexOfArray+1; i--) { 
                ((UIDragButton *)[_buttonArray objectAtIndex:i]).frame = ((UIDragButton *)[_buttonArray objectAtIndex:i-1]).frame; 
            } 
            ((UIDragButton *)[_buttonArray objectAtIndex:i]).frame = _frameRect; 
        }];


在block中只需要直接赋值即可实现效果。


若使用CABasicAnimation则较为复杂

定义动画

- (CABasicAnimation *)moveX:(float)time X:(NSNumber *)x  // 横向移动 
{ 
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 
    animation.toValue=x; 
    animation.duration=time;                    // 动画持续时间 
    animation.removedOnCompletion=NO; 
    animation.fillMode=kCAFillModeForwards; 
    animation.delegate = self; 
    return animation; 
}


使用动画

[self.layer addAnimation:[self moveX:0.1 X:[NSNumber numberWithFloat:x]] forKey:nil];


委托需要协议<NSObject>



评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!