如何处理UIPanGestureRecognizer和UISwipeGestureRecognizer(上和下)

问题描述:

我需要使视图像iPhone屏幕(不完全是这个,但表现手势的刷卡上/下和UIPanesture)屏幕截图附加。 查看这里的代码。对我而言,只有工作Pangesture,我需要两个工作? see screen shot如何处理UIPanGestureRecognizer和UISwipeGestureRecognizer(上和下)

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let pan = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan(sender:))) 
    pan.maximumNumberOfTouches = 1 
    pan.minimumNumberOfTouches = 1 
    slideView.addGestureRecognizer(pan) 

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:))) 
    swipeUp.direction = UISwipeGestureRecognizerDirection.up 
    slideView.addGestureRecognizer(swipeUp) 

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:))) 
    swipeDown.direction = UISwipeGestureRecognizerDirection.down 
    slideView.addGestureRecognizer(swipeDown) 
    print(self.slideView.frame.origin.y) 

} 
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 
@IBAction func respondToSwipeGesture(gesture: UIGestureRecognizer) { 

    if let swipeGesture = gesture as? UISwipeGestureRecognizer { 


     switch swipeGesture.direction { 
     case UISwipeGestureRecognizerDirection.right: 
      print("Swiped right") 
     case UISwipeGestureRecognizerDirection.down: 
      print("Swiped down") 
      animateSlideView() 
     case UISwipeGestureRecognizerDirection.left: 
      print("Swiped left") 
     case UISwipeGestureRecognizerDirection.up: 
      print("Swiped up") 
      animateSlideView() 
     default: 
      break 
     } 
    } 
} 



@IBAction func slideView(_ sender: Any) { 

    animateSlideView() 

} 

func animateSlideView(){ 
    UIView.animate(withDuration: 0.7, animations: { 
     if self.toggleTop == true{ 
      self.toggleTop = false 
      self.slideView.frame = CGRect(x: 0, y: 670, width: self.slideView.frame.size.width, height: self.slideView.frame.size.height) 
     }else { 
      self.toggleTop = true 
      self.slideView.frame = CGRect(x: 0, y: 150, width: self.slideView.frame.size.width, height: self.slideView.frame.size.height) // CGRectMake(0, 670, self.slideView.frame.size.width, self.slideView.frame.size.height) 
     } 
     self.slideView.layoutIfNeeded() 
    }) 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


@IBAction func handlePan(sender: UIPanGestureRecognizer) { 
    var touchPointOffset:CGPoint = CGPoint(x: 0, y: 0) 
    let vel = sender.velocity(in: self.view) 
    //print(vel.y) 
    print(sender.location(in: slideView).y) 
    let actualPosition = sender.view!.center.y - 319.0 
    let translation = sender.translation(in: self.view) 
    // [gestureRecognizer locationInView:self.panelViewController.view].y < 
    if (sender.state == .began || sender.state == .changed) { 
     if actualPosition > 670 { 

     } else if actualPosition < 150 { 

     } else{ 
      sender.view!.center = CGPoint(x: sender.view!.center.x , y: sender.view!.center.y + translation.y) 
     } 
     touchPointOffset = sender.location(in: slideView) 
     //self.touchPointOffset = sender.location(in: slideView) //[gestureRecognizer locationInView:self.panelViewController.view]; 
    }else if sender.state == .cancelled{ 
     print("canceled") 
    }else if sender.state == .ended{ 
     print(sender.location(in: slideView).y) 
     //print(vel.y) 
     touchPointOffset = CGPoint(x: 0, y: 0) 
     if (vel.y > 0.0) { 
      if actualPosition >= 670 || actualPosition >= UIScreen.main.bounds.height/2.0 { 
       UIView.animate(withDuration: 0.7, animations: { 
        self.toggleTop = false 
        sender.view!.center = CGPoint(x: sender.view!.center.x , y: 989.0) 
        self.slideView.layoutIfNeeded() 
       }) 

      }else if actualPosition <= 150 || actualPosition <= UIScreen.main.bounds.height/2.0{ 
       UIView.animate(withDuration: 0.7, animations: { 
        self.toggleTop = true 
        sender.view!.center = CGPoint(x: sender.view!.center.x , y: 469.0) 
        self.slideView.layoutIfNeeded() 
       }) 

      } 
     } 

     //print(vel.y) 

    } 

    sender.setTranslation(CGPoint(x: 0,y :0), in: self.view) 
} 
+0

您是否尝试过在这两种方法中设置断点? – user3581248

您正在使用的shouldRecognizeSimultaneouslyWithGestureRecognizer委托方法从UIGestureRecognizerDelegate,但它似乎没有,好像你已经设置你的ViewController以符合该委托。第一:

class ViewController: UIViewController, UIGestureRecognizerDelegate { 

} 

然后确保delegate财产您需要的UIGestureRecogniers同时被公认的被设置为self(您的ViewController):

override func viewDidLoad() { 
    super.viewDidLoad() 

    let pan = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan(sender:))) 
    pan.delegate = self  // HERE - for the delegate 
    pan.maximumNumberOfTouches = 1 
    pan.minimumNumberOfTouches = 1 
    slideView.addGestureRecognizer(pan) 

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:))) 
    swipeUp.delegate = self  // HERE - for the delegate 
    swipeUp.direction = UISwipeGestureRecognizerDirection.up 
    slideView.addGestureRecognizer(swipeUp) 

    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:))) 
    swipeDown.delegate = self  // HERE - for the delegate 
    swipeDown.direction = UISwipeGestureRecognizerDirection.down 
    slideView.addGestureRecognizer(swipeDown) 
    print(self.slideView.frame.origin.y) 

} 
+0

谢谢,它的工作原理和很多更好的解决方案。做得好。 –

+0

@HumzaShahid - 不客气!既然它有效,你会介意接受它作为正确的答案,所以我得到信用?祝你好运。 – Pierce