CATransitionを使用してページの「左から右へ」「右から左へ」のアニメーションを実装

2287 ワード

-(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"];
}