2017-02-17 76 views
0

我需要使視圖像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

回答

0

您正在使用的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

相關問題