更改帧后,UIContainerView停止接收触摸

时间:2017-11-15 13:54:25

标签: ios swift uiview uicontainerview

UIContainerView显示视图控制器:

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

        }
    }
}

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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