官方文件
Core Animation Extensions To Key-Value Coding
實作的部分,就沿用前一篇的專案來作修改
1. 在StoryBoard上加入一些Button
2. 在mainViewController.m加入所需程式碼。
#import "mainViewController.h"#import "CALayerAdditions.h"
@interface mainViewController ()
@end
#define ORGINAL_POSITION CGPointMake(350, 450)
#define MOVED_POSITON CGPointMake(450, 350)
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
@implementation mainViewController
@synthesize layer, actionsSwitch;
@synthesize layer2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 200, 200);
layer.position = ORGINAL_POSITION; ///CGPointMake(500, 300);
layer.backgroundColor = [UIColor redColor].CGColor;
layer.borderColor = [UIColor blackColor].CGColor;
layer.opacity = 1.0f;
[self.view.layer addSublayer:layer];
actionsSwitch.on = NO;
UIImage *image = [UIImage imageNamed:@"TaiwanMap.jpg"];
layer2 = [CALayer layer];
layer2.contents = (id)image.CGImage;
layer2.bounds = CGRectMake(0, 0, 200, 200);
layer2.position = CGPointMake(350, 650);
[self.view.layer addSublayer:layer2];
//UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fadeIt)];
//[self.view addGestureRecognizer:recognizer];
}
/*
- (void)fadeIt {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.toValue = [NSNumber numberWithFloat:0.0];
animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
animation.duration = 3.0;
layer2.opacity = 0.0; // This is required to update the model's value. Comment out to see what happens.
[layer2 addAnimation:animation forKey:@"animateOpacity"];
}
*/
- (IBAction)opacity:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.toValue = [NSNumber numberWithFloat:0.0];
animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
animation.duration = 3.0;
layer2.opacity = 0.0; // This is required to update the model's value. Comment out to see what happens.
[layer2 addAnimation:animation forKey:@"animateOpacity"];
}
- (IBAction)translationX:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animation.toValue = [NSNumber numberWithFloat:100.0];
//animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
// [imageLayer addAnimation:[self animationOpen] forKey:@"Open"];
}
- (IBAction)translationY:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.toValue = [NSNumber numberWithFloat:100.0];
//animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (IBAction)translationZ:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.z"];
animation.toValue = [NSNumber numberWithFloat:100.0];
//animation.fromValue = [NSNumber numberWithFloat:layer.opacity];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (IBAction)scale:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.toValue = [NSNumber numberWithFloat:2.0];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (IBAction)rotationX:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
animation.toValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (IBAction)rotationY:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.toValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (IBAction)rotationZ:(id)sender {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.toValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(120))];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.duration = 3.0;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
[layer2 addAnimation:animation forKey:@"Open"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toggleCorner:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
layer.cornerRadius = (layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
}
- (IBAction)toggleColor:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
CGColorRef redColor = [UIColor redColor].CGColor, blueColor = [UIColor blueColor].CGColor;
layer.backgroundColor = (layer.backgroundColor == redColor) ? blueColor : redColor;
}
- (IBAction)toggleOpacity:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
layer.opacity = (layer.opacity == 1.0f) ? 0.5f : 1.0f;
}
- (IBAction)togglePosition:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
layer.position = layer.position.x == 500 ? MOVED_POSITON : ORGINAL_POSITION;
}
- (IBAction)toggleBorders:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
layer.borderWidth = (layer.borderWidth == 0.0f) ? 10.0f : 0.0f;
}
- (IBAction)toggleBounds:(id)sender {
[CATransaction setDisableActions:actionsSwitch.on];
[layer adjustWidthBy:layer.bounds.size.width == layer.bounds.size.height ? 100 : -100];
}
@end









沒有留言:
張貼留言