#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
CALayer* layer1;
CALayer* layer2;
CALayer* layer3;
//目前的轉置矩陣
CATransform3D currentTransform;
}
@property (retain, nonatomic) IBOutlet UIView *bgLayer;
- (IBAction)onYellowUp:(id)sender;
- (IBAction)onGreenUp:(id)sender;
- (IBAction)onRotateY:(id)sender;
@end
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCALayer];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)setupCALayer{
layer1 =[[CALayer layer]retain];
layer1.bounds = CGRectMake(0, 0, 100, 100);
layer1.position = CGPointMake(100, 100);
layer1.borderColor = [UIColor redColor].CGColor;
layer1.borderWidth = 10;
layer1.backgroundColor = [UIColor yellowColor].CGColor;
layer1.cornerRadius = 15;
layer1.opacity = 0.8;
[self.bgLayer.layer addSublayer:layer1];
layer2 =[[CALayer layer]retain];
layer2.bounds = CGRectMake(0, 0, 100, 100);
layer2.position = CGPointMake(120, 120);
layer2.borderColor = [UIColor blueColor].CGColor;
layer2.borderWidth = 10;
layer2.backgroundColor = [UIColor greenColor].CGColor;
layer2.cornerRadius = 15;
layer2.opacity = 0.8;
[self.bgLayer.layer addSublayer:layer2];
//增加z軸
layer2.zPosition = -5;
//模擬3D動畫
CATransform3D identity = CATransform3DIdentity;
identity.m34 = 1/-50;
self.bgLayer.layer.sublayerTransform = identity;
currentTransform = identity;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_bgLayer release];
[super dealloc];
}
- (IBAction)onYellowUp:(id)sender {
layer1.zPosition = 0;
layer2.zPosition = -5;
}
- (IBAction)onGreenUp:(id)sender {
layer1.zPosition = -5;
layer2.zPosition = 0;
}
- (IBAction)onRotateY:(id)sender {
CABasicAnimation* rotate = [CABasicAnimation animationWithKeyPath:@"sublayerTransform"];
CATransform3D newTransform = CATransform3DRotate(currentTransform, M_PI/6, 0, 1, 0);
rotate.toValue = [NSValue valueWithCATransform3D:newTransform];
rotate.duration = 2;
rotate.fillMode = kCAFillModeBoth;
rotate.removedOnCompletion = NO;
[self.bgLayer.layer addAnimation:rotate forKey:nil];
currentTransform = newTransform;
}
@end
沒有留言:
張貼留言