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

标签 ios swift uiviewanimation

我的 View 是设备屏幕宽度的两倍,并且具有滑动手势识别器,可以向左或向右移动 View 。但我开始意识到,如果在最初不在屏幕上的 View 部分上执行滑动,则 View 将不会执行手势操作。

// 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

    })
}

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

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

最佳答案

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

所以我认为你的问题是

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

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

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

    关于ios - 将手势识别器添加到屏幕外的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600452/

    相关文章:

    ios - 无法识别的选择器在JSON解析中发送到实例

    ios - 来自静态分析器的 API 滥用错误

    swift - 在 Swift 中 didFinishPickingMediaWithInfo 之前如何知道在 photoLibrary 中选择的媒体类型?

    swift - 如何将一个电话号码添加到多个实体

    ios - 动画不会在 UITableView 的 backgroundView 上被杀死

    ios - getTokenForcingRefresh 在 Firebase 中已弃用。用什么代替它?

    ios - 推送 View 时 UISearchController 不会被关闭

    ios - RXSwift flatMap 两种方法

    objective-c - 如何在不调用 `layoutIfNeeded` 的情况下为约束设置动画?

    swift - 为什么 UIView 和 UIImageView 的动画效果不同?