簡體   English   中英

將手勢識別器添加到屏幕外的元素

[英]Add Gesture Recognizer to element off-screen

我的視圖的寬度是設備屏幕的兩倍,並且具有滑動手勢識別器,可以左右移動視圖。 但是我已經意識到,如果在最初不在屏幕上的視圖的一部分上執行滑動,則該視圖將不會執行手勢動作。

// 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個像素上無法識別手勢。

我不太了解您的回答,但希望對您有所幫助

所以我認為你的問題是

  1. 您聲明滑動功能的方式是錯誤的。 代替func swipeLeft()將其更改為func swipeLeft(sender:UISwipeGestureRecognizer)

要么

  1. 問題可能是您聲明框架變量的方式。 您可以在我的代碼中查看已更改的代碼。

。碼。

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

    })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM