#import
"
ViewController.h
"
@interface ViewController ()
@property (nonatomic,strong)CALayer *imageLayer;
@end
@implementation ViewController
- ( void)viewDidLoad {
[super viewDidLoad];
UIImage *image1 = [UIImage imageNamed: @" test1.jpg "];
// 可以顯示圖片類容,layer是view的載體
// self.view.layer.contents = (__bridge id)(image.CGImage);
// 創建出圖片layer
self.imageLayer = [CALayer layer];
self.imageLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.imageLayer];
self.imageLayer.contents = (__bridge id)(image1.CGImage);
[self performSelector:@selector(imageAnimation) withObject:nil afterDelay: 3];
}
- ( void)imageAnimation {
// 隱式動畫
UIImage *image2 = [UIImage imageNamed: @" test2.jpg "];
// self.imageLayer.contents = (__bridge id)(image2.CGImage);
CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath: @" contens "];
contentsAnimation.fromValue = self.imageLayer.contents; // 原始圖片
contentsAnimation.toValue = (__bridge id)(image2.CGImage); // 切換后圖片
contentsAnimation.duration = 3.f;
CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath: @" bounds "];
boundsAnimation.fromValue = [NSValue valueWithCGRect:self.imageLayer.bounds];
boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectMake( 10, 100, 200, 200)];
boundsAnimation.duration = 3.f;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[contentsAnimation,boundsAnimation];
group.duration = 3.f;
// 設置layer動畫結束后的值否則會回復東湖錢的狀態
self.imageLayer.contents = (__bridge id)(image2.CGImage);
self.imageLayer.bounds = CGRectMake( 10, 100, 200, 200);
[self.imageLayer addAnimation:group forKey:nil];
}
@interface ViewController ()
@property (nonatomic,strong)CALayer *imageLayer;
@end
@implementation ViewController
- ( void)viewDidLoad {
[super viewDidLoad];
UIImage *image1 = [UIImage imageNamed: @" test1.jpg "];
// 可以顯示圖片類容,layer是view的載體
// self.view.layer.contents = (__bridge id)(image.CGImage);
// 創建出圖片layer
self.imageLayer = [CALayer layer];
self.imageLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.imageLayer];
self.imageLayer.contents = (__bridge id)(image1.CGImage);
[self performSelector:@selector(imageAnimation) withObject:nil afterDelay: 3];
}
- ( void)imageAnimation {
// 隱式動畫
UIImage *image2 = [UIImage imageNamed: @" test2.jpg "];
// self.imageLayer.contents = (__bridge id)(image2.CGImage);
CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath: @" contens "];
contentsAnimation.fromValue = self.imageLayer.contents; // 原始圖片
contentsAnimation.toValue = (__bridge id)(image2.CGImage); // 切換后圖片
contentsAnimation.duration = 3.f;
CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath: @" bounds "];
boundsAnimation.fromValue = [NSValue valueWithCGRect:self.imageLayer.bounds];
boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectMake( 10, 100, 200, 200)];
boundsAnimation.duration = 3.f;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[contentsAnimation,boundsAnimation];
group.duration = 3.f;
// 設置layer動畫結束后的值否則會回復東湖錢的狀態
self.imageLayer.contents = (__bridge id)(image2.CGImage);
self.imageLayer.bounds = CGRectMake( 10, 100, 200, 200);
[self.imageLayer addAnimation:group forKey:nil];
}