使用CATransition实现页面的“从左向右” “从右向左”的动画

时间:2021-10-03 11:19:48
-(void)initView{
UISwipeGestureRecognizer *left_gesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(action:)];
[left_gesture setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *right_gesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(action:)];
[right_gesture setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:left_gesture];
[self.view addGestureRecognizer:right_gesture];
} -(void)action:(UISwipeGestureRecognizer *)gesture{
CATransition *animation = [CATransition animation];
[animation setValue:@"swipe" forKey:@"name"];
animation.type = kCATransitionReveal;
if (gesture.direction==UISwipeGestureRecognizerDirectionLeft) {
animation.subtype = kCATransitionFromRight;
[self.view setBackgroundColor:[UIColor redColor]];
}else if(gesture.direction==UISwipeGestureRecognizerDirectionRight){
animation.subtype = kCATransitionFromLeft;
[self.view setBackgroundColor:[UIColor blueColor]];
} [self.view.layer addAnimation:animation forKey:@"animation"];
}