OGeek|极客世界-中国程序员成长平台

标题: ios - UIContainerView 在更改框架后停止接收触摸 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:18
标题: ios - UIContainerView 在更改框架后停止接收触摸

UIContainerView 显示一个 View Controller :

enter image description here

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

@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

        }
    }
}

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

enter image description here



Best Answer-推荐答案


这是一个说明如何将变换应用于 super View 中检测到的触摸点的 Playground :

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

关于ios - UIContainerView 在更改框架后停止接收触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47309244/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4