【问题标题】:UIContainerView stops receiving touch after changing frameUIContainerView 在更改框架后停止接收触摸
【发布时间】:2018-04-28 18:52:32
【问题描述】:

UIContainerView 显示一个视图控制器:

现在,当用户使用此代码点击全屏按钮时,我将使其全屏显示:

@IBAction func fullscreen(_ sender: Any) {
        view.goFullscreen()
    }

extension CGAffineTransform {

    static let ninetyDegreeRotation = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
}

extension UIView {

    var fullScreenAnimationDuration: TimeInterval {
        return 0.15
    }

    func minimizeToFrame(_ frame: CGRect) {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.layer.setAffineTransform(.identity)
            self.frame = frame
        }
    }


    func goFullscreen() {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.layer.setAffineTransform(.ninetyDegreeRotation)
            self.frame = UIScreen.main.bounds

        }
    }
}

使视图全屏工作正常,但当视图控制器全屏时,视图停止接收触摸。当我向视图控制器添加约束时会发生这种情况,但是当我删除它时它工作正常。为什么会发生这种情况,我怎样才能同时设置约束和接收触摸?

【问题讨论】:

  • 不幸的是,手势识别器忽略了您的转换。因此,您需要手动将转换应用于接触点。它们有很多方法可以做到这一点,例如将识别器应用于超级视图,对点应用逆变换,然后检查它是否在目标视图的边界内。
  • @JoshHomann 谢谢!你能告诉我一些代码吗?
  • 我在火车上。回家后我会添加代码。基本上问题是你的变换只影响你在屏幕上的明显位置,而不是你的实际边界。这就是为什么你应该真正将变换用于动画之类的暂时性事物,并使用约束来进行永久的大小/位置更改。在这种情况下,我会考虑在此处使用 layoutifneeded 动画约束的变化,而不是使用变换。
  • @JoshHomann 你好,你回家了吗?我真的需要解决这个问题

标签: ios swift uiview uicontainerview


【解决方案1】:

这是一个说明如何将变换应用于超级视图中检测到的触摸点的游乐场:

import UIKit
import PlaygroundSupport

class ViewController: UIViewController {
    private let redView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(redView)
        redView.translatesAutoresizingMaskIntoConstraints = false
        redView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        redView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        redView.centerXAnchor.constraint(equalTo: redView.superview!.centerXAnchor).isActive = true
        redView.centerYAnchor.constraint(equalTo: redView.superview!.centerYAnchor).isActive = true
        redView.backgroundColor = .red
        redView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tap))
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    @objc private func tap(tapGestureRecognizer: UITapGestureRecognizer) {
        let point = tapGestureRecognizer.location(in: redView).applying(redView.transform)
        if redView.bounds.applying(redView.transform).contains(point) {
            print ("You tapped inside")
        } else {
            print("You tapped outside)")
        }
    }
}

let v = ViewController()
PlaygroundPage.current.liveView = v.view

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多