如果我按下按钮然后旋转我的设备,我的程序就不再有效了。我知道,手势识别器是问题,但我该如何解决呢? 我将手势识别器添加到我的视图中,如果设备在横向模式下旋转,则手势识别器不再工作。有什么办法可以解决这个问题? 这是一些代码:
// take a photo
@IBAction func takePhoto(sender: AnyObject) {
self.fullScreenView.hidden = false
self.recordButton.enabled = false
self.takephoto.enabled = false
self.recordButton.hidden = true
self.takephoto.hidden = true
session.startRunning()
currentGesture = "Photo"
// customize the quality level or bitrate of the output photo
session.sessionPreset = AVCaptureSessionPresetPhoto
// add the AVCaptureVideoPreviewLayer to the view and set the view in fullscreen
fullScreenView.frame = view.bounds
videoPreviewLayer.frame = fullScreenView.bounds
fullScreenView.layer.addSublayer(videoPreviewLayer)
// add action to fullScreenView
gestureFullScreenView = UITapGestureRecognizer(target: self, action: #selector(ViewController.takePhoto(_:)))
self.fullScreenView.addGestureRecognizer(gestureFullScreenView)
// add action to myView
gestureView = UITapGestureRecognizer(target: self, action: #selector(ViewController.setFrontpage(_:)))
self.view.addGestureRecognizer(gestureView)
if (preview == true) {
if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
// code for photo capture goes here...
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
// process the image data (sampleBuffer) here to get an image file we can put in our view
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let image = UIImage(data: imageData, scale: 1.0)
var orientation: UIImageOrientation!
// image with the current orientation
let newImage = UIImage(CGImage: (image?.CGImage)!, scale: 1, orientation: orientation)
// remove the gesture recognizer and stop the current session
self.fullScreenView.hidden = true
self.fullScreenView.gestureRecognizers?.forEach(self.fullScreenView.removeGestureRecognizer)
self.session.stopRunning()
// save image to the library
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
self.imageViewBackground = UIImageView(frame: self.view.bounds)
self.imageViewBackground.image = newImage
self.imageViewBackground.tag = self.key
self.view.addSubview(self.imageViewBackground)
}
})
}
}
else {
preview = true
}
}
在这里我设置了新框架:
override func viewWillLayoutSubviews() {
// if the device rotates, set these components into fullscreen
fullScreenView.frame = view.bounds
videoPreviewLayer?.frame = view.bounds
playerLayer?.frame = view.bounds
imageViewBackground?.frame = view.bounds
let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
switch (orientation) {
case .Portrait:
videoPreviewLayer?.connection.videoOrientation = .Portrait
countLabel.frame = CGRectMake(width * 0.4, -(width * 0.24), height/2, (width/2) * 1.25)
case .LandscapeRight:
videoPreviewLayer?.connection.videoOrientation = .LandscapeLeft
countLabel.frame = CGRectMake((width * 0.72), -(width * 0.26), height/2, (width/2) * 1.25)
case .LandscapeLeft:
videoPreviewLayer?.connection.videoOrientation = .LandscapeRight
countLabel.frame = CGRectMake((width * 0.72), -(width * 0.26), height/2, (width/2) * 1.25)
case .PortraitUpsideDown:
videoPreviewLayer?.connection.videoOrientation = .PortraitUpsideDown
countLabel.frame = CGRectMake(width * 0.4, -(width * 0.24), height/2, (width/2) * 1.25)
default:
videoPreviewLayer?.connection.videoOrientation = .Portrait
countLabel.frame = CGRectMake(width * 0.4, -(width * 0.24), height/2, (width/2) * 1.25)
}
}