繁体   English   中英

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

[英]Add Gesture Recognizer to element 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

    })
}

因此,在向左滑动查看视图的另一半(在屏幕外加载)后,我无法向右滑动。 不能在屏幕外加载的位置执行手势吗?

编辑:因此,经过一番修补后,我看到在屏幕外加载的视图的任何位置都无法识别该手势。 因此,如果我移动视图以查看从屏幕外开始的50个像素,则在该50个像素上无法识别手势。

我不太了解您的回答,但希望对您有所帮助

所以我认为你的问题是

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

要么

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

。码。

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