iOS转场动画

转场动画

ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor orangeColor];

//view   --->  view

//CAAnimation
//NSOperation

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

self.imageView.image = [UIImage imageNamed:@"1.jpg"];

[self.view addSubview:self.imageView];

}

-(void)touchesBegan:(NSSet UITouch > )touches withEvent:(UIEvent *)event
{
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@”%d.jpg”,arc4random() % 2 + 1]];

//
CATransition *ann = [CATransition animation];

//设置动画类型
ann.type = @"push";

//
ann.subtype = kCATransitionFromRight;

ann.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

//设置动画的时间
ann.duration = 2.0;

//添加动画
[self.imageView.layer addAnimation:ann forKey:@"key"];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (IBAction)click:(id)sender {
    CYViewController *vc = [[CYViewController alloc] init];
    CATransition *ann = [CATransition animation];

    //设置动画类型
    ann.type = @”rippleEffect”;

    //
    ann.subtype = kCATransitionFromLeft;

    ann.duration = 0.25;

    [self.navigationController.view.layer addAnimation:ann forKey:@”key”];

    [self.navigationController pushViewController:vc animated:NO];
    }
    @end

CYViewController.m

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor purpleColor];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:0 target:self action:@selector(click)];
    }

-(void)click
{
CATransition *ann = [CATransition animation];

//设置动画类型
ann.type = @"rippleEffect";

//
ann.subtype = kCATransitionFromRight;

ann.duration = 0.25;

[self.navigationController.view.layer addAnimation:ann forKey:@"key"];

[self.navigationController popViewControllerAnimated:NO];

}

猜你喜欢

转载自blog.csdn.net/weixin_35966617/article/details/53084136