2016-11-15 61 views
0

我有一個視圖,它的寬度是我的設備屏幕寬度的兩倍,並具有向左或向右移動視圖的滑動手勢識別器。但是我已經認識到,如果在原本不在屏幕上的部分視圖上執行輕掃,視圖將不會執行手勢操作。將手勢識別器添加到屏幕外的元素

// View twice as wide as the device screen 
let masterView = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width * 2, height: 100)) 

let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(ThisViewController.swipeLeft)) 
let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(ThisViewController.swipeRight)) 
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.left 
swipeRightGesture.direction = UISwipeGestureRecognizerDirection.right 
masterView.addGestureRecognizer(swipeLeftGesture) 
masterView.addGestureRecognizer(swipeRightGesture) 
self.addSubview(masterView) 


func swipeLeft() {   
    // Moves masterView to the left equal to that of the width of the screen (or half of masterView's total width) 
    let moveLeftFrame = CGRect(x: (masterView.frame.width/2) * -1, y: masterView.frame.minY, width: masterView.frame.width, height: masterView.frame.height) 

    UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: { 
     UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: { 
      masterView.frame = moveLeftFrame 
     }) 
    }, completion: { (true) in 

    }) 
} 


func swipeRight() {   
    // Moves masterView back to original location 
    let moveRightFrame = CGRect(x: 0, y: masterView.frame.minY, width: masterView.frame.width, height: masterView.frame.height) 

    UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: { 
     UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: { 
      masterView.frame = moveRightFrame 
     }) 
    }, completion: { (true) in 

    }) 
} 

於是往左滑動來查看視圖(其上裝載關屏)的另一半後,我無法刷卡回到正確的。不能在已加載屏幕的位置執行手勢操作?

編輯:所以在修補了一下之後,我發現手勢無法識別在加載離屏的視圖的任何位置。因此,如果我移動視圖以查看從屏幕外開始的50個像素,則無法在該50個像素上識別手勢。

回答

0

我真的不明白你的答案,但希望這可以幫助

因此,我認爲你的問題是

  1. 你聲明你刷卡功能是錯誤的方式。相反FUNC swipeLeft的()將其更改爲FUNC swipeLeft(發件人:UISwipeGestureRecognizer)

  • 問題可能是你在你的聲明框架變量的方式。您可以在我的代碼中查看更改後的一個。
  • .code。

    func swipeLeft(sender: UISwipeGestureRecognizer) {   
        // Moves masterView to the left equal to that of the width of the screen (or half of masterView's total width) 
        let moveLeftFrame = CGRect(x: -view.frame.width, y: 0, width: masterView.frame.width, height: masterView.frame.height) 
    
        UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: { 
         UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: { 
          masterView.frame = moveLeftFrame 
         }) 
        }, completion: { (true) in 
    
        }) 
    } 
    
    func swipeRight(sender: UISwipeGestureRecognizer) {   
        // Moves masterView back to original location 
        let moveRightFrame = CGRect(x: 0, y: 0, width: masterView.frame.width, height: masterView.frame.height) 
    
        UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: .calculationModeCubic, animations: { 
         UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.4, animations: { 
          masterView.frame = moveRightFrame 
         }) 
        }, completion: { (true) in 
    
        }) 
    } 
    
    +0

    視圖移動工作正常。只是在最初不可見的視圖上拖動任何地方都無法識別手勢 – Tamarisk

    +0

    如何拖動不可見的一面。我認爲你的意思是你想拖動屏幕的其餘部分而不是圖像。那麼我建議你的手勢添加到你的'視圖'而不是'masterView' –