ViewController.m
#import "ViewController.h"
#import "ImageAnimationViewController.h"
@interface ViewController ()
//黑色视图
@property (nonatomic, strong) UIView *blackView;
//导航栏的按钮点击事件
- (IBAction)nextVC:(id)sender;
//初始化界面
- (void)initUserInterface;
#pragma mark - UIView动画
////手势方法
//- (void)tapProcess;
////位移
//- (void)positionAnimation;
////缩放
//- (void)scaleAnimation;
////旋转
//- (void)rotationAnimation;
////变色
//- (void)colorAnimation;
////透明度
//- (void)alphAnimation;
////结束
//- (void)endAnimation;
////UIView动画回调方法
//- (void)showViewAnimationDidStop:(NSString *)animationID;
#pragma mark - block方法
//位移
- (void)positionAnimationBlock;
//缩放
- (void)scaleAnimationBlock;
//旋转
- (void)rotationAnimationBlock;
//颜色
- (void)colorAnimationBlock;
//透明度
- (void)alphAnimationBlock;
//返回
- (void)endAnimationBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUserInterface];
}
#pragma mark - initUserInterface method
- (void)initUserInterface
{
//黑色背景视图
self.view.backgroundColor = [UIColor whiteColor];
_blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
_blackView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_blackView];
/**< 为黑色背景视图添加点击手势 */
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapProcess)];
[_blackView addGestureRecognizer:tapGesture];
}
#pragma mark - 手势方法
- (void)tapProcess{
[self positionAnimationBlock];
}
#pragma block动画
//位移
- (void)positionAnimationBlock{
[UIView animateWithDuration:0.5 animations:^{
_blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
} completion:^(BOOL finished) {
[self scaleAnimationBlock];
}];
}
//缩放
- (void)scaleAnimationBlock{
[UIView animateWithDuration:0.5 animations:^{
_blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
} completion:^(BOOL finished) {
[self rotationAnimationBlock];
}];
}
//旋转
- (void)rotationAnimationBlock{
[UIView animateWithDuration:0.5 animations:^{
_blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
} completion:^(BOOL finished) {
[self colorAnimationBlock];
}];
}
//变色
- (void)colorAnimationBlock
{
[UIView animateWithDuration:0.5 animations:^{
_blackView.backgroundColor = [UIColor redColor];
} completion:^(BOOL finished) {
[self alphAnimationBlock];
}];
}
//透明度(渐变)
- (void)alphAnimationBlock
{
[UIView animateWithDuration:0.5 animations:^{
_blackView.alpha = 0.5;
} completion:^(BOOL finished) {
[self endAnimationBlock];
}];
}
//结束
- (void)endAnimationBlock{
[UIView animateWithDuration:1 animations:^{
//视图恢复到初始状态
_blackView.backgroundColor = [UIColor blackColor];
_blackView.alpha = 1.0;
_blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
// CGAffineTransformIdentity:移除所有的变幻属性
_blackView.transform = CGAffineTransformIdentity;
//转场动画(效果)
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
} completion:^(BOOL finished) {
}];
}
#pragma mark - UIView动画
////位移
//- (void)positionAnimation{
// //开始动画
// [UIView beginAnimations:@"position" context:nil];
// //持续时间
// [UIView setAnimationDuration:1];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
// //核心动画
// _blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
// //提交动画
// [UIView commitAnimations];
//}
//
////缩放
//- (void)scaleAnimation{
// //开始动画
// [UIView beginAnimations:@"scale" context:nil];
// //持续时间
// [UIView setAnimationDuration:0.7];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//
// //核心动画
// /**< 设置动画类型:缩放
// transform:变化属性(可实现旋转和所缩放动画)
// CGAffineTransformScale...:在新生成的属性变换基础之上继续执行
// CGAffineTransformMakeScale...:在最初始(最原始)的属性基础上做变化 */
//
// _blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
//
// //在变化后的基础上还可持续放大
// //_blackView.transform = CGAffineTransformScale(_blackView.transform, 1.5, 1.5);
// //提交动画
// [UIView commitAnimations];
//}
//
////旋转
//- (void)rotationAnimation{
// //开始动画
// [UIView beginAnimations:@"rotation" context:nil];
// //持续时间
// [UIView setAnimationDuration:0.5];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//
// // 设置动画类型:旋转
// // angle:角度
// // M_PI:180°
// // M_PI_2:90°
// // M_PI_4:45°
// //旋转之后缩回原来的大小
// //_blackView.transform = CGAffineTransformMakeRotation(M_PI);
// //旋转之后保持放大后的大小
// _blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
// //提交动画
// [UIView commitAnimations];
//
//}
//
////变色
//- (void)colorAnimation{
// //开始动画
// [UIView beginAnimations:@"color" context:nil];
// //持续时间
// [UIView setAnimationDuration:0.5];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
// //核心动画
// _blackView.backgroundColor = [UIColor redColor];
// //提交动画
// [UIView commitAnimations];
//}
//
////透明度
//- (void)alphAnimation{
// //开始动画
// [UIView beginAnimations:@"alph" context:nil];
// //持续时间
// [UIView setAnimationDuration:0.5];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
// //核心动画
// _blackView.alpha = 0.5;
// //提交动画
// [UIView commitAnimations];
//
//}
//
////结束
//- (void)endAnimation{
// //开始动画
// [UIView beginAnimations:@"end" context:nil];
// //持续时间
// [UIView setAnimationDuration:1];
// //线性规律
// [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// //添加代理
// [UIView setAnimationDelegate:self];
// //设置回调方法
// [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//
// //视图恢复到初始状态
// _blackView.backgroundColor = [UIColor blackColor];
// _blackView.alpha = 1.0;
// _blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
//
// // CGAffineTransformIdentity:移除所有的变幻属性
// _blackView.transform = CGAffineTransformIdentity;
//
//
// //转场动画(效果)
// [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
//
// //提交动画
// [UIView commitAnimations];
//}
//
//#pragma mark - UIView动画回调方法
//- (void)showViewAnimationDidStop:(NSString *)animationID{
// if ([animationID isEqualToString:@"position"]) {
// [self scaleAnimation];
// }else if ([animationID isEqualToString:@"scale"]){
// [self rotationAnimation];
// }else if ([animationID isEqualToString:@"rotation"]){
// [self colorAnimation];
// }else if ([animationID isEqualToString:@"color"]){
// [self alphAnimation];
// }else if ([animationID isEqualToString:@"alph"]){
// [self endAnimation];
// }
//
//}
- (IBAction)nextVC:(id)sender {
ImageAnimationViewController *imageVC = [[ImageAnimationViewController alloc]init];
[self.navigationController pushViewController:imageVC animated:YES];
}
@end
ImageAnimationViewController.m
#import "ImageAnimationViewController.h"
#define IMAGE_WITH_NAME(name)[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]]
#define SCREEN_WEIGH [UIScreen mainScreen].bounds.size.width
@interface ImageAnimationViewController ()
@property (nonatomic, strong) UIImageView *imageView;
- (void)initUserInterface;
@end
@implementation ImageAnimationViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUserInterface];
}
- (void)initUserInterface{
self.view.backgroundColor = [UIColor cyanColor];
//
NSMutableArray *imageArray = [NSMutableArray array];
//创建图片数组
for (int i = 0; i < 138; i ++) {
NSString *imageName = [NSString stringWithFormat:@"%d",i];
//第一种
// UIImage *image = [UIImage imageNamed:imageName];
//第二种(加载大量的图片)
// NSString *pathString = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
// UIImage *image = [UIImage imageWithContentsOfFile:pathString];
//第三种(宏定义)
UIImage *image = IMAGE_WITH_NAME(imageName);
[imageArray addObject:image];
}
//创建图片视图
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WEIGH, 400)];
[self.view addSubview:imageView];
//为其添加图片数组
imageView.animationImages = imageArray;
//动画时长
imageView.animationDuration = 2;
self.imageView = imageView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 80, 60);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"stop" forState:UIControlStateNormal];
[button addTarget:self action:@selector(stopProcess) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)stopProcess{
[self.imageView stopAnimating];
}
//获取点击屏幕动作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1. 获取touch
UITouch *touch = [touches anyObject];
// 2. 获取当期点
CGPoint point = [touch locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(point));
// 3. 动画开始
[_imageView startAnimating];
}
- (void)viewWillDisappear:(BOOL)animated{
//animationImages数组无法释放,所以需要手动释放,但是不能直接写 self.catImageView.animationImages = nil;因为动画是异步,还没执行完,数组就释放会崩溃的。所以可以使用performSelector让动画在刚好做完后延迟释放,或者在viewWillDisappear
self.imageView.animationImages = nil;
}
@end