【问题标题】:Add Gesture Recognizer to element off-screen将手势识别器添加到屏幕外的元素
【发布时间】: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


    【解决方案1】:

    我不太明白你的回答,但希望这会有所帮助

    所以我认为你的问题是

    1. 您声明滑动功能的方式是错误的。而不是 func swipeLeft() 将其更改为 func swipeLeft(sender: UISwipeGestureRecognizer)

    1. 问题可能出在您声明框架变量的方式上。您可以在我的代码中查看更改的内容。

    .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
    
        })
    }
    

    【讨论】:

    • 视图移动工作正常。只是在最初不可见的视图上拖动任何位置都无法识别手势
    • 如何在不可见的一侧拖动。我认为您的意思是您想拖动屏幕的其余部分而不仅仅是图像。那么我的建议是将您的手势添加到您的“视图”而不是“masterView”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多