|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | #import "ViewController.h"
|
| 10 | +#import <QuartzCore/QuartzCore.h> |
10 | 11 |
|
11 | 12 | @interface ViewController ()
|
12 |
| - |
| 13 | +@property (weak, nonatomic) IBOutlet UIView *containerView; |
| 14 | +@property (nonatomic) CALayer *top; |
| 15 | +@property (nonatomic) CALayer *bottom; |
| 16 | +@property (nonatomic) BOOL open; |
13 | 17 | @end
|
14 | 18 |
|
15 | 19 | @implementation ViewController
|
16 | 20 |
|
17 | 21 | - (void)viewDidLoad
|
18 | 22 | {
|
19 | 23 | [super viewDidLoad];
|
20 |
| - // Do any additional setup after loading the view, typically from a nib. |
| 24 | + |
| 25 | + self.view.backgroundColor = [UIColor blackColor]; |
| 26 | + |
| 27 | + CGSize viewSize = self.containerView.bounds.size; |
| 28 | + self.top = [CALayer layer]; |
| 29 | + self.bottom = [CALayer layer]; |
| 30 | + self.top.anchorPoint = CGPointMake(0.5, 0.5); |
| 31 | + self.bottom.anchorPoint = CGPointMake(0.5, 0.5); |
| 32 | + self.top.position = CGPointMake(viewSize.width/2.0, viewSize.height/2.0); |
| 33 | + self.bottom.position = CGPointMake(viewSize.width/2.0, viewSize.height/2.0); |
| 34 | + self.top.bounds = CGRectMake(0, 0, viewSize.width, viewSize.height); |
| 35 | + self.bottom.bounds = CGRectMake(0, 0, viewSize.width, viewSize.height); |
| 36 | + self.top.backgroundColor = [UIColor whiteColor].CGColor; |
| 37 | + self.bottom.backgroundColor = [UIColor whiteColor].CGColor; |
| 38 | + |
| 39 | + self.top.transform = makePerspectiveTransform(); |
| 40 | + self.bottom.transform = makePerspectiveTransform(); |
| 41 | + |
| 42 | + [self.containerView.layer addSublayer:self.top]; |
| 43 | + [self.containerView.layer addSublayer:self.bottom]; |
| 44 | + |
| 45 | + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]; |
| 46 | + [self.containerView addGestureRecognizer:tap]; |
| 47 | + |
| 48 | + self.open = NO; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +- (void)handleTap { |
| 53 | + if (!self.open) { |
| 54 | + UIImage *image = [UIImage imageNamed:@"imageTest.jpg"]; |
| 55 | + |
| 56 | + CGImageRef imgRef = image.CGImage; |
| 57 | + self.top.contents = (__bridge id)imgRef; |
| 58 | + self.bottom.contents = (__bridge id)imgRef; |
| 59 | + self.top.contentsRect = CGRectMake(0.0, 0.0, 1.0, 0.5); |
| 60 | + self.bottom.contentsRect = CGRectMake(0.0, 0.5, 1.0, 0.5); |
| 61 | + |
| 62 | + self.top.transform = CATransform3DScale(self.top.transform, 0.95, 0.95, 0.95); |
| 63 | + self.bottom.transform = CATransform3DScale(self.bottom.transform, 0.95, 0.95, 0.95); |
| 64 | + |
| 65 | + self.top.transform = CATransform3DRotate(self.top.transform, M_PI_2, 1.0, 0.0, 0.0); |
| 66 | + self.bottom.transform = CATransform3DRotate(self.bottom.transform, -M_PI_2, 1.0, 0.0, 0.0); |
| 67 | + |
| 68 | + self.top.transform = CATransform3DTranslate(self.top.transform, 0.0, 0.0, 0.0); |
| 69 | + self.bottom.transform = CATransform3DTranslate(self.bottom.transform, 0.0, 0.0, 0.0); |
| 70 | + } else { |
| 71 | + self.top.transform = CATransform3DIdentity; |
| 72 | + self.bottom.transform = CATransform3DIdentity; |
| 73 | + self.top.transform = makePerspectiveTransform(); |
| 74 | + self.bottom.transform = makePerspectiveTransform(); |
| 75 | + } |
| 76 | + self.open = !self.open; |
21 | 77 | }
|
22 | 78 |
|
23 |
| -- (void)didReceiveMemoryWarning |
| 79 | +CATransform3D makePerspectiveTransform() |
24 | 80 | {
|
25 |
| - [super didReceiveMemoryWarning]; |
26 |
| - // Dispose of any resources that can be recreated. |
| 81 | + CATransform3D transform = CATransform3DIdentity; |
| 82 | + transform.m34 = 1.0 / -2000; |
| 83 | + return transform; |
27 | 84 | }
|
28 | 85 |
|
29 | 86 | @end
|
0 commit comments