iOS Core Animation (本人写的源码)

来源:互联网 发布:我国对外投资数据 编辑:程序博客网 时间:2024/06/14 22:14

//

//  ViewController.h

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@end


//  ViewController.m

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import "ViewController.h"

#import "SecondViewController.h"



NSString * const string =@"123";



@interface ViewController ()


@property (strong,nonatomic) IBOutletUIImageView *imageView;

@property (strong,nonatomic) IBOutletUIImageView *colorImageView;

@property(nonatomic,retain)SecondViewController *secondVC;




@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.imageView.image=[UIImageimageNamed:@"10042637.jpg"];

    self.imageView.layer.cornerRadius=20;

    self.imageView.layer.borderWidth=2;

    self.imageView.layer.borderColor=[UIColorgreenColor].CGColor;

    self.imageView.layer.shadowColor=[[UIColorblueColor] CGColor];

    self.imageView.layer.shadowOffset=CGSizeMake(20,20);

    self.imageView.layer.shadowOpacity=0.5;

    [self.imageViewsetClipsToBounds:YES];

    //超过主层边框范围的内容都裁剪

    self.imageView.layer.masksToBounds=NO;

    

    //[self testImageView];

    

    self.colorImageView.backgroundColor=[UIColorpurpleColor];

    self.colorImageView.layer.cornerRadius=20;

    self.colorImageView.layer.borderWidth=2;

    self.colorImageView.layer.borderColor=[UIColorgreenColor].CGColor;

    self.colorImageView.layer.shadowColor=[[UIColoryellowColor]CGColor];

    self.colorImageView.layer.shadowOffset=CGSizeMake(20,20);

    self.colorImageView.layer.shadowOpacity=0.5;

    [self.colorImageViewsetClipsToBounds:YES];

    self.colorImageView.layer.masksToBounds=NO;


    self.secondVC=[self.storyboardinstantiateViewControllerWithIdentifier:@"secondVC"];

}


-(void)testImageView{

    //[self.imageView.layer setValue:@(M_PI_4) forKeyPath:@"transform.rotation"];

//    NSValue *value=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];

//    [self.imageView.layer setValue:value forKeyPath:@"transform"];

    //[self.imageView.layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)] forKeyPath:@"transform"];

   // [self.imageView.layer setValue:[NSNumber numberWithFloat:0.5] forKeyPath:@"transform.scale.y"];

    [self.imageView.layersetValue:@(0.5)forKeyPath:@"transform.scale.y"];

}


//UIView动画的动画块方法

- (IBAction)propertyAnimation:(UIButton *)sender {

    [UIViewbeginAnimations:@"AnimationOne"context:nil];

    [UIViewsetAnimationDuration:1];

    [UIViewsetAnimationRepeatCount:2];

    //[UIView setAnimationDelegate:self];  这个有什么用??

    self.colorImageView.bounds=CGRectMake(120,80, 100, 100);

    self.colorImageView.layer.cornerRadius=50;

    self.colorImageView.transform=CGAffineTransformMakeRotation(M_PI);

    [UIViewcommitAnimations];

}


//UIView动画的block方法

- (IBAction)blockAnimation:(UIButton *)sender {

    [UIViewanimateWithDuration:1delay:1options:UIViewAnimationOptionTransitionFlipFromBottomanimations:^{

        self.colorImageView.bounds=CGRectMake(0,0, 200, 200);

        self.colorImageView.transform=CGAffineTransformMakeRotation(2*M_PI);

    } completion:^(BOOL finished) {

        

    }];

}



- (IBAction)transmitAnimation:(UIButton *)sender {

    UIView *view=self.view.superview;

    [UIViewbeginAnimations:@"transmit"context:nil];

    [UIViewsetAnimationDuration:3];

    [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view.superviewcache:YES];

    [self.viewremoveFromSuperview];

    [view addSubview:self.secondVC.view];

    [UIViewcommitAnimations];

    

}


//UIViewTransitionAPI方法

- (IBAction)blockTransmitAnimation:(UIButton *)sender {

    [UIViewtransitionFromView:self.viewtoView:self.secondVC.viewduration:1options:UIViewAnimationOptionTransitionCurlUpcompletion:^(BOOL finished) {

        

    }];

    UIView *view=self.view.superview;

    [UIViewtransitionWithView:view duration:1options:UIViewAnimationOptionTransitionCurlUpanimations:^{

        [self.viewremoveFromSuperview];

        [view addSubview:self.secondVC.view];

    } completion:^(BOOL finished) {

        

    }];

}


//CAAnimation是抽象类,CAPropertyAnimation-->CABasicAnimation

- (IBAction)basicAnimation:(UIButton *)sender {

    CABasicAnimation *basicAnimation=[CABasicAnimationanimationWithKeyPath:@"backgroundColor"];

    basicAnimation.duration=6;

        basicAnimation.fromValue = (__bridgeid)([[UIColorredColor] CGColor]);

        basicAnimation.toValue = (__bridgeid)([[UIColorpurpleColor] CGColor]);

//        basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];

//        basicAnimation.byValue = [NSNumber numberWithFloat:0.2];

    [self.colorImageView.layeraddAnimation:basicAnimation forKey:@"haha"];

    self.colorImageView.backgroundColor=[UIColoryellowColor];

}


//CAAnimation是抽象类,CAPropertyAnimation-->CAKeyFrameAnimation

- (IBAction)keyFrameAnimation:(UIButton *)sender {

    CAKeyframeAnimation *keyFrameAnimation=[CAKeyframeAnimationanimationWithKeyPath:@"position.x"];

    CGFloat center=self.colorImageView.layer.position.x;

    CGFloat left=center-20;

    CGFloat right=center+20;

    NSNumber *c=[NSNumbernumberWithFloat:center];

    NSNumber *l=[NSNumbernumberWithFloat:left];

    NSNumber *r=[NSNumbernumberWithFloat:right];

    keyFrameAnimation.values=@[c,r,l,c,l,r,c,r,l,r,c,l];

    [self.colorImageView.layeraddAnimation:keyFrameAnimation forKey:@"shake"];

}


//CATransition,作⽤:layer的过渡动画

//有两个主要属性:type(设置过渡动画的效果)subType(设置过渡动画的⽅方向)

- (IBAction)coreAnimation:(UIButton *)sender {

    CATransition *transition=[CATransitionanimation];

    transition.duration=3;

    transition.startProgress=0.5;

    transition.type=@"cube";

    transition.subtype=kCATransitionFromLeft;

    UIView *view =self.view.superview;

    [view.layer addAnimation:transitionforKey:@"过渡"];

    [self.viewremoveFromSuperview];

    [view addSubview:self.secondVC.view];

}


//CAAnimation是抽象类,CAAnimationGroup-->CAAnimationGroup只有⼀一个数组属性,可以添加多个 CAAnimation,⼀一起执⾏行

- (IBAction)groupAnimation:(UIButton *)sender {

    CAAnimationGroup *groupAnimation=[CAAnimationGroupanimation];

    

    CABasicAnimation *basicAnimation=[CABasicAnimationanimationWithKeyPath:@"position"];

    basicAnimation.fromValue=[NSValuevalueWithCGPoint:self.colorImageView.layer.position];

    basicAnimation.toValue=[NSValuevalueWithCGPoint:CGPointMake(50,300)];

 

    

    CABasicAnimation *basicAnimation1=[CABasicAnimationanimationWithKeyPath:@"color"];

    basicAnimation1.fromValue = (__bridgeid)([[UIColorredColor] CGColor]);

    basicAnimation1.toValue = (__bridgeid)([[UIColorpurpleColor] CGColor]);


    

    groupAnimation.duration=3;

    groupAnimation.animations=@[basicAnimation,basicAnimation1];

    [self.colorImageView.layeraddAnimation:groupAnimation forKey:@"2"];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


//  FirstViewController.h

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface FirstViewController : UIViewController


@end

//  FirstViewController.m

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import "FirstViewController.h"

#import "Macro.h"

#import "ViewController.h"


@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfbootAnimation];

}

-(void)bootAnimation{

    UIImageView *bootImageView=[[UIImageViewalloc] initWithFrame:[UIScreenmainScreen].bounds];

    bootImageView.image=[UIImageimageNamed:@"zhiyan"];

    [self.viewaddSubview:bootImageView];

    [UIViewanimateWithDuration:3delay:0options:UIViewAnimationOptionOverrideInheritedDurationanimations:^{

        bootImageView.frame=CGRectMake(-30, -50,kScreen_Width+60,kScreen_Height+100);

    } completion:^(BOOL finished) {

        [bootImageView removeFromSuperview];

        ViewController *viewC=[self.storyboardinstantiateViewControllerWithIdentifier:@"viewVC"];

        //[self.view removeFromSuperview];

        [self.viewaddSubview:viewC.view];

    }];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

//

//  SecondViewController.h

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import <UIKit/UIKit.h>


extern NSString *const string;


@interface SecondViewController : UIViewController


@end


//  SecondViewController.m

//  Animation_byEpisode

//

//  Created by Wangyun on 15/7/9.

//  Copyright (c) 2015 com.epsiode. All rights reserved.

//


#import "SecondViewController.h"

#import "ViewController.h"


@interface SecondViewController ()

@property(nonatomic,retain)ViewController *viewController;

@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"string :%@",string);

    

    _viewController=[self.storyboardinstantiateViewControllerWithIdentifier:@"viewVC"];

}


- (IBAction)swiftBack:(UIButton *)sender {

    [UIViewtransitionFromView:self.viewtoView:self.viewController.viewduration:1options:UIViewAnimationOptionTransitionCurlDowncompletion:^(BOOL finished) {

        

    }];

    UIView *view=self.view.superview;

    [UIViewtransitionWithView:view duration:1options:UIViewAnimationOptionTransitionCurlDownanimations:^{

        [self.viewremoveFromSuperview];

        [view addSubview:self.viewController.view];

    } completion:^(BOOL finished) {

        

    }];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



0 0
原创粉丝点击