Last active
December 21, 2015 16:09
-
-
Save xdream86/6331803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
@interface ViewController () { | |
CALayer* _myLayer; | |
BOOL _isShowed; | |
BOOL _isAnimationRunning; | |
} | |
@property (nonatomic, retain) CABasicAnimation* myAnimation1; | |
@property (nonatomic, retain) CABasicAnimation* myAnimation2; | |
@end | |
@implementation ViewController | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)viewDidLoad{ | |
[super viewDidLoad]; | |
_myLayer = [[CALayer alloc] init]; | |
_myLayer.contents = (id)[[UIImage imageNamed:@"a.png"] CGImage]; | |
_myLayer.bounds = CGRectMake(0, 0, self.view.bounds.size.width / 2, self.view.bounds.size.height); | |
_myLayer.anchorPoint = CGPointMake(0, 0.5); | |
_myLayer.position = CGPointMake(0, self.view.bounds.size.height / 2); | |
/** | |
* 给图层设置右上角和右下角的圆角 | |
*/ | |
UIBezierPath *maskPath; | |
maskPath = [UIBezierPath bezierPathWithRoundedRect:_myLayer.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; | |
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; | |
maskLayer.frame = _myLayer.bounds; | |
maskLayer.path = maskPath.CGPath; | |
_myLayer.mask = maskLayer; | |
/** | |
* 给图层设置默认的视角和反转角度,默认为-45度。 | |
*/ | |
CATransform3D perspective = CATransform3DIdentity; | |
perspective.m34 = 1.0 / 500.0; | |
CATransform3D rotation = CATransform3DRotate(perspective, - M_PI_4, 0, 1, 0); | |
_myLayer.transform = rotation; | |
[self.view.layer addSublayer:_myLayer]; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (CABasicAnimation*) myAnimation1 { | |
if (!_myAnimation1) { | |
_myAnimation1 = [CABasicAnimation | |
animationWithKeyPath:@"transform.rotation.y"]; | |
_myAnimation1.delegate = self; | |
_myAnimation1.fromValue = @( - M_PI_4); | |
_myAnimation1.toValue = @(0); | |
_myAnimation1.removedOnCompletion = YES; | |
_myAnimation1.duration = .5; | |
_myAnimation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; | |
} | |
return _myAnimation1; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (CABasicAnimation*) myAnimation2 { | |
if (!_myAnimation2) { | |
_myAnimation2 = [CABasicAnimation | |
animationWithKeyPath:@"transform.rotation.y"]; | |
_myAnimation2.delegate = self; | |
_myAnimation2.fromValue = @(0); | |
_myAnimation2.toValue = @(- M_PI_4); | |
_myAnimation2.removedOnCompletion = YES; | |
_myAnimation2.duration = .5; | |
_myAnimation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; | |
} | |
return _myAnimation2; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void) display { | |
// First we update the model layer's property. | |
CATransform3D perspective = CATransform3DIdentity; | |
perspective.m34 = 1.0 / 500.0; | |
CATransform3D rotation = CATransform3DRotate(perspective, M_PI * 2, 0, 1, 0); | |
_myLayer.transform = rotation; | |
// Now we attach the animation. | |
[_myLayer addAnimation:self.myAnimation1 forKey:@"animation1"]; | |
_isShowed = YES; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void) disappear { | |
// First we update the model layer's property. | |
CATransform3D perspective = CATransform3DIdentity; | |
perspective.m34 = 1.0 / 500.0; | |
CATransform3D rotation = CATransform3DRotate(perspective, - M_PI_4, 0, 1, 0); | |
_myLayer.transform = rotation; | |
// Now we attach the animation. | |
[_myLayer addAnimation:self.myAnimation2 forKey:@"animation2"]; | |
_isShowed = NO; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (IBAction)actionPressed:(id)sender { | |
if (_isAnimationRunning) return; | |
_isShowed ? [self disappear] : [self display]; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { | |
_isAnimationRunning = NO; | |
NSLog(@"animation stop"); | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)animationDidStart:(CAAnimation *)theAnimation { | |
_isAnimationRunning = YES; | |
NSLog(@"animation start"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment