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

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

 

posted @ 2015-01-21 10:45  爱生活爱代码  阅读(943)  评论(0编辑  收藏  举报