iPad Playground中的两个视图控制器具有不同的View.Frame CGRects

时间:2019-03-21 06:33:26

标签: ios swift xcode swift-playground

我正在iPad上使用Swift游乐场。我创建了两个ViewController,由于某种原因,它们都有不同的框架。

VC1的帧为0,0,1114,626

AND

VC2的帧为0,0,1194,834

这是我的代码:

import UIKit
import ARKit
import Vision
import PlaygroundSupport

class VC1: UIViewController, ARSCNViewDelegate, ARSessionDelegate {

// MARK: Create Objects
var sceneView: ARSCNView!
let vc2 = VC2()
let extractImageButton = UIView()

// MARK: Delegate Methods
override func loadView() {
    super.loadView()
    configureAR()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    sceneView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // View Configurations
    extractImageButton.backgroundColor = UIColor(red: 199/255, green: 178/255, blue: 149/255, alpha: 1)
    extractImageButton.layer.cornerRadius = 28
    extractImageButton.layer.borderColor = UIColor.white.cgColor
    extractImageButton.layer.borderWidth = 3
    extractImageButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(takePhoto)))
    extractImageButton.translatesAutoresizingMaskIntoConstraints = false

    // Add Subviews
    view.addSubview(extractImageButton)

    // View Constraints
    extractImageButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    extractImageButton.widthAnchor.constraint(equalToConstant: 56).isActive = true
    extractImageButton.heightAnchor.constraint(equalToConstant: 56).isActive = true
    extractImageButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -48).isActive = true
}

// MARK: Functions

func configureAR() {
    sceneView = ARSCNView(frame: .zero)
    sceneView.delegate = self
    sceneView.session.delegate = self
    sceneView.automaticallyUpdatesLighting = true
    let worldTrackingConfiguration = ARWorldTrackingConfiguration()
    worldTrackingConfiguration.planeDetection = .horizontal
    sceneView.session.run(worldTrackingConfiguration)
    self.view = sceneView
}

@objc func takePhoto() {
    if let currentFrame = sceneView.session.currentFrame {
        print(view.frame)
        let cvPixelBuffer = currentFrame.capturedImage
        var ciImage = CIImage(cvPixelBuffer: cvPixelBuffer)
        let ciImageCroppedToCgRect = CGRect(x: 0, y: (ciImage.extent.height-(626/1114)*1920)/2, width: 1920, height: (626/1114)*1920)
        let ciImageTranslation = CGAffineTransform(translationX: 0, y: (-(ciImage.extent.height-(626/1114)*1920)/2))
        let croppedCiImage = ciImage.cropped(to: ciImageCroppedToCgRect).transformed(by: ciImageTranslation)
        vc2.inputImage = croppedCiImage
        self.present(vc2, animated: false, completion: nil)
    }
}
}

class VC2: UIViewController {

// MARK: Create Objects
var inputImage: CIImage!

let imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = UIView.ContentMode.scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

// MARK: Delegate Methods
override func loadView() {
    super.loadView()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // View Configurations
    view.backgroundColor = .orange
    imageView.image = UIImage(ciImage: inputImage)
    print(view.frame)

    // Add Subviews
    view.addSubview(imageView)

    // View Constraints
    imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

// MARK: Functions

}

PlaygroundPage.current.liveView = VC1()
PlaygroundPage.current.needsIndefiniteExecution = true

为什么两个视图控制器具有不同的框架?任何想法,我将不胜感激。

0 个答案:

没有答案