动画总结(UIView的动画)
Main.storyboard
ViewController.m
//
// ViewController.m
// 8A08.动画总结
//
// Created by huan on 16/2/6.
// Copyright © 2016年 huanxi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//打印中心点 position anchorPoint
#warning 默认图层的"position" 就是“控件”的中心点
NSLog(@"中心点 %@", NSStringFromCGPoint(self.imageView.center));
NSLog(@"position %@", NSStringFromCGPoint(self.imageView.layer.position));
NSLog(@"anchorPoint %@", NSStringFromCGPoint(self.imageView.layer.anchorPoint));
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//动画总结
// [self test1];
// [self test2];
// [self test3];
// [self test4];
}
//1. UIView动画
-(void)test1{
[UIView beginAnimations:nil context:nil];
//设置时间
[UIView setAnimationDuration:3];
//监听动画的完成
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stop)];
//实现动画代码
self.imageView.center = CGPointMake(200, 200);
[UIView commitAnimations];
}
//2.UIView的block动画
-(void)stop{
NSLog(@"%s", __func__);
}
-(void)test2{
[UIView animateWithDuration:3 animations:^{
self.imageView.center = CGPointMake(200, 200);
} completion:^(BOOL finished) {
NSLog(@"动画完成");
}];
}
//3.UIView的转场动画
-(void)test3{
// UIViewAnimationOptionTransitionNone = 0 << 20, // default
// UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
// UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
// UIViewAnimationOptionTransitionCurlUp = 3 << 20,
// UIViewAnimationOptionTransitionCurlDown = 4 << 20,
// UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
// UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
// UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
[UIView transitionWithView:self.imageView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
//更改图片
self.imageView.image = [UIImage imageNamed:@"2.jpg"];
} completion:^(BOOL finished) {
NSLog(@"动画完成");
}];
}
//核心动画 核心动画是一个假象
-(void)test4{
// self.imageView.image = [UIImage imageNamed:@"2.jpg"];
// //转场动画
// CATransition *animation = [CATransition animation];
// animation.type = @"push";
//
// [self.imageView.layer addAnimation:animation forKey:nil];
//核心动画是一个假象 给你一个效果(效果之后就恢复)
//平移动画
CABasicAnimation *positionAni = [CABasicAnimation animation];
positionAni.keyPath = @"position";
NSLog(@"核心动画之前的position %@", NSStringFromCGPoint(self.imageView.layer.position));
positionAni.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
//核心动画 要监听动画完成 设置代理
#warning 核心动画的代理是NSObject的分类,所以不需要遵守协议
positionAni.delegate = self;
[self.imageView.layer addAnimation:positionAni forKey:nil];
}
#pragma mark 核心动画的代理
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
// NSLog(@"动画完成");
NSLog(@"核心动画之后的position %@", NSStringFromCGPoint(self.imageView.layer.position));
}
@end