【发布时间】:2016-11-15 09:45:57
【问题描述】:
我有一个比我的设备屏幕宽两倍的视图,并且有可以左右移动视图的滑动手势识别器。但我开始意识到,如果在最初不在屏幕上的视图部分执行滑动操作,视图将不会执行手势操作。
// 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 像素上识别手势。
【问题讨论】:
标签: ios swift uiviewanimation