5.视图层的动画效果

动画实现也是属于V部分的逻辑,这点的理由有这么两个:

  • 动画实现和演示视图存在依赖关系

  • 将动画实现放到视图层可以实现动效视图的复用

话是这么说,但是在许多的项目中,这样的代码比比皆是:

@implementation ViewController: UIViewController

//弹窗动画效果
- (void)animatedAlertView {
    AlertView *alert = [[AlertView alloc] initWithMessage: @"这是一条弹窗警告信息"];
    alert.alpha = 0;
    alert.center = self.view.center;
    alert.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration: 0.25 animations: ^{
        alert.alpha = 1;
        alert.transform = CGAffineTransformIdentity;
    }];
}

@end

具体的槽点笔者就不吐了,对于动画实现笔者只有一个建议:无论你要实现的动画多么简单,统一扔到View中去实现,提供接口给C层调用展示。要知道,饱受开发者吐槽的UIAlertView在弹窗效果上的接口简洁的挑不出毛病,仅仅一个- (void)show就完成了众多的动画效果。如果你不喜欢因为动画效果就要自定义视图,那么将常用的动画效果以category的方式扩展出来使用:

@interface UIView (Animation)

- (void)pop;

@end

@implementation UIView (Animation)

- (void)pop {
    CGPoint center = CGPointMake(self.superView.frame.size.width / 2, self.superView.frame.size.height / 2);
    self.center = center;
    self.alpha = 0;
    self.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration: 0.25 animations: ^{
        self.alpha = 1;
        self.transform = CGAffineTransformIdentity;
    }];
}

@end
文章导航