I have a view that is twice as wide as my device screen and have swiping gesture recognizers that move the view either left or right. But I have come to realize that the view will not perform the gesture action if the swipe is performed on a portion of the view that was initially 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
})
}
So after swiping to the left to see the other half of the view (which is loaded off-screen), I am unable to swipe back to the right. Can gestures not be performed on a location that was loaded off-screen?
EDIT: So after tinkering a bit, I see that the gesture is not recognized on any location of the view that is loaded off-screen. So if I move the view to see, say, 50 pixels that started off off-screen, gestures aren't being recognized on that 50 pixels.
I don't really understand your answer but hopefully this helps
So what I think your problem is
or
.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
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.