Main.storyboard動畫
ViewController.matom
//url
// ViewController.mspa
// 8A08.動畫總結代理
//blog
// Created by huan on 16/2/6.圖片
// Copyright © 2016年 huanxi. All rights reserved.ip
//animation
#import "ViewController.h"it
@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