菜鸟教程小白 发表于 2022-12-11 19:18:10

ios - UIContainerView 在更改框架后停止接收触摸


                                            <p><p>UIContainerView 显示一个 ViewController :</p>

<p> <a href="/image/u6wj6.jpg" rel="noreferrer noopener nofollow"><img src="/image/u6wj6.jpg" alt="enter image description here"/></a> </p>

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

<pre><code>@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

      }
    }
}
</code></pre>

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

<p> <a href="/image/8btzB.jpg" rel="noreferrer noopener nofollow"><img src="/image/8btzB.jpg" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是一个说明如何将变换应用于 superView 中检测到的触摸点的 Playground :</p>

<pre><code>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 (&#34;You tapped inside&#34;)
      } else {
            print(&#34;You tapped outside)&#34;)
      }
    }
}

let v = ViewController()
PlaygroundPage.current.liveView = v.view
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIContainerView 在更改框架后停止接收触摸,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47309244/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47309244/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIContainerView 在更改框架后停止接收触摸