## 視圖層的動畫效果
動畫實現也是屬于`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
~~~