简体   繁体   English

将手势识别器添加到屏幕外的元素

[英]Add Gesture Recognizer to element off-screen

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. 因此,如果我移动视图以查看从屏幕外开始的50个像素,则在该50个像素上无法识别手势。

I don't really understand your answer but hopefully this helps 我不太了解您的回答,但希望对您有所帮助

So what I think your problem is 所以我认为你的问题是

  1. the way you're declaring your swiping function is wrong. 您声明滑动功能的方式是错误的。 Instead of func swipeLeft() change it to func swipeLeft(sender: UISwipeGestureRecognizer) 代替func swipeLeft()将其更改为func swipeLeft(sender:UISwipeGestureRecognizer)

or 要么

  1. the problem could be the way you're declaring your frame variables. 问题可能是您声明框架变量的方式。 you can view the changed one in my code. 您可以在我的代码中查看已更改的代码。

.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

    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM