左侧滑动推视图控制器

时间:2017-04-24 12:48:38

标签: ios pushviewcontroller

如何使用panGesture向左滑动(向左平移)推动视图控制器? 目前使用SwipeRightToPopController {{1}}库来实现平移右侧的pop视图控制器。

2 个答案:

答案 0 :(得分:1)

试试这个

在Viewdidload中

 let leftSwipe = UISwipeGestureRecognizer(target: self, action:  #selector(handleSwipes) )
 let rightSwipe = UISwipeGestureRecognizer(target: self, action:#selector(handleSwipes) )

 leftSwipe.direction = .left
 rightSwipe.direction = .right

 view.addGestureRecognizer(leftSwipe)
 view.addGestureRecognizer(rightSwipe)

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .left)
    {
        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "FirstViewController") as UIViewController
        self.navigationController?.pushViewController(initialViewControlleripad, animated: false)
    }if(sender.direction == .right){

       let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "SecondViewController") as UIViewController
        self.navigationController?.pushViewController(initialViewControlleripad, animated: false)
     }
}

答案 1 :(得分:0)

试试这个:

在ViewDidLoad中:

UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;// change direction as per your needs

其选择方法:

-(void)handleSwipeGesture:(UIGestureRecognizer *) sender
{
    NSUInteger touches = sender.numberOfTouches;
    if (touches == 2)
    {
        if (sender.state == UIGestureRecognizerStateEnded)
        { 
            //push view controller here    
        }
    }  
}

使用Pan Gesture

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[self.view addGestureRecognizer:panRecognizer];

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint velocity = [panRecognizer velocityInView:self.view];

    NSLog(@"Pan Detected.");

    if (velocity.x > 0)
    {
        NSLog(@"Pan went right.");
    }
    else
    {
        NSLog(@"Pan went left.");     

        if (panRecognizer.state == UIGestureRecognizerStateChanged)
            NSLog(@"push here ");

        [self performSegueWithIdentifier:@"Settings" sender:panRecognizer];
    }
}