我使用了一些UISwipeGestureRecognizer作为菜单的选择器。因为我有不止一个,所以我需要有一个有限的滑动检测区域。这是我使用的代码:
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe1"))
rightSwipe.direction = .right
view.addGestureRecognizer(rightSwipe)我试过将它添加到UIImageView中,但它无法检测到触摸。
发布于 2017-02-05 22:31:45
默认情况下,UIImage和UILabel的isUserInteractionEnabled为false。将其设置为true。
据我所知,你不能限制手势区域。但是你可以检查手势的位置,并决定如何处理它。
例如:
func addGesture() {
let rightGesture = UISwipeGestureRecognizer(target: self, action: #selector(someAction(sender:)))
rightGesture.direction = .right
let leftGesture = UISwipeGestureRecognizer(target: self, action: #selector(someAction(sender:)))
leftGesture.direction = .right
view.addGestureRecognizer(rightGesture)
view.addGestureRecognizer(leftGesture)
}
func someAction(sender: UISwipeGestureRecognizer) {
let location = sender.location(in: view)
if location.x > view.frame.width / 2 && sender.direction == .right {
// do something
} else if location.x < view.frame.width / 2 && sender.direction == .left {
// do something else
}
}发布于 2017-02-05 22:24:37
我只需要添加.isUserInteractionEnabled = true。下面是我的代码:
var imageswipe = UIImageView()
imageswipe.backgroundColor = UIColor.red
imageswipe.frame = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height / 4)
imageswipe.center = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes"))
rightSwipe.direction = .right
imageswipe.isUserInteractionEnabled = true
imageswipe.addGestureRecognizer(rightSwipe)
view.addSubview(imageswipe)https://stackoverflow.com/questions/42053138
复制相似问题