版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.12.12 |
前言
如果你細看了我前面寫的有關動畫的部分,就知道前面介紹了
CoreAnimation
、序列幀以及LOTAnimation
等很多動畫方式,接下來幾篇我們就以動畫示例為線索,進行動畫的講解。相關代碼已經上傳至GitHub - 刀客傳奇。感興趣的可以看我寫的前面幾篇。
1. 動畫示例(一) —— 一種外擴的簡單動畫
功能需求
實現抖動的那種動畫,很多APP比如在做刪除等操作時候,會進行抖動動畫以提醒用戶。
功能實現
下面還是先看代碼。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *alertButton;
@end
@implementation ViewController
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
alertButton.frame = CGRectMake(self.view.bounds.size.width * 0.5 - 50.0, self.view.bounds.size.width * 0.5 - 50.0, 100.0, 100.0);
[alertButton addTarget:self action:@selector(alertButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
alertButton.backgroundColor = [UIColor redColor];
[self.view addSubview:alertButton];
self.alertButton = alertButton;
}
#pragma mark - Object Private Function
- (void)alertButtonDidClick:(UIButton *)button
{
button.selected = !button.selected;
if (button.selected) {
srand([[NSDate date] timeIntervalSince1970]);
float rand=(float)random();
CFTimeInterval t=rand*0.0000000001;
[UIView animateWithDuration:0.1 delay:t options:0 animations:^
{
self.alertButton.transform = CGAffineTransformMakeRotation(-0.05);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^
{
self.alertButton.transform = CGAffineTransformMakeRotation(0.05);
} completion:^(BOOL finished) {}
];
}];
}
else {
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^
{
self.alertButton.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
}
}
@end
功能效果
下面我們就看一下實現的效果。
后記
未完,待續~~~