[英]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個像素上無法識別手勢。
我不太了解您的回答,但希望對您有所幫助
所以我認為你的問題是
要么
。碼。
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.