gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:54:20 25 4
gpt4 key购买 nike

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

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com