iOS 动画Animation详解, UIView动画(UIView属性动画,UIViewTransition动画,UIView Block动画),CALayer动画(CABasicAnima, CAKeyframeAnimation, CATransition, CAAnimationGroup)
#import "FirstVC.h"
@interface FirstVC ()
@end
@implementation FirstVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)pressPropertyAnimation:(id)sender {
[UIView beginAnimations:@"改变大小" context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(startAnimation)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatCount:20];
[UIView setAnimationRepeatAutoreverses:YES];
self.changeView.frame = CGRectMake(110, 100, 100, 100);
self.changeView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
[UIView commitAnimations];
}
- (IBAction)pressTranstionAnimation:(id)sender {
[UIView beginAnimations:@"过度动画" context:NULL];
[UIView setAnimationDuration:5];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.changeView cache:YES];
self.changeView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
[UIView commitAnimations];
}
- (IBAction)pressBlockAnimation:(id)sender {
[UIView transitionWithView:self.changeView duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
self.changeView.backgroundColor = [UIColor orangeColor];
} completion:^(BOOL finished) {
if (finished) {
NSLog(@"finished");
}
}];
}
#pragma mark - AnimationDelegate
- (void)animationWillStart:(NSString *)animationID context:(void *)context
{
NSLog(@"start: %@, %@", animationID, context);
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
NSLog(@"stop: %@, %@", animationID, context);
}
- (void)startAnimation
{
NSLog(@"self");
}
- (void)dealloc {
[_changeView release];
[super dealloc];
}
@end
#import "SecondVC.h"
@interface SecondVC ()
@end
@implementation SecondVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", NSStringFromCGRect(self.changeView.frame));
NSLog(@"%f", CGRectGetWidth(self.changeView.frame));
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[_changeView release];
[super dealloc];
}
- (IBAction)pressBasicAnimation:(id)sender {
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
[basic setDuration:2];
basic.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
[self.changeView.layer addAnimation:basic forKey:@"changColor"];
}
- (IBAction)pressKeyFrameAnimation:(id)sender {
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
[keyFrame setDuration:10];
keyFrame.values = @[(id)[UIColor redColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor];
keyFrame.keyTimes = @[@(0.3), @(0.5), @(0.6), @(0.7), @(0.9)];
[self.changeView.layer addAnimation:keyFrame forKey:nil];
}
- (IBAction)pressTransition:(id)sender {
CATransition *transition = [CATransition animation];
[transition setDuration:2];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromTop;
[self.changeView.layer addAnimation:transition forKey:nil];
}
- (IBAction)pressAnimationGroup:(id)sender {
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
[basic setDuration:2];
basic.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
[keyFrame setDuration:5];
keyFrame.values = @[(id)[UIColor redColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor];
keyFrame.keyTimes = @[@(0.3), @(0.5), @(0.6), @(0.7), @(0.9)];
CAAnimationGroup *group = [CAAnimationGroup animation];
[group setDuration:10];
group.animations = @[basic, keyFrame];
[self.changeView.layer addAnimation:group forKey:nil];
}
@end
转载出处:http://www.2cto.com/kf/201409/335661.html