Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.

Commit fc6b05c

Browse files
committed
project 4 reviewed
CameraViewController 中没有用到 UIGestureRecognizerDelegate 中的方法。相关声明和 delegate 赋值可以去掉。 didPressTakeAnother 方法,改为didTouchScreen更合适,因为里面要处理两种case,TakeAnother不能涵盖函数的功能,也不够明确。 另外里面没有必要调用captureSession.startRunning()。 didPressTakePhoto改为takePhoto。拍摄照片和触发拍摄的操作没有关系,不需要在函数名中体现press。 AVCaptureSession应该在需要时才启动。长时间开启摄像头会增加耗电,让设备发热,影响摄像头寿命。
1 parent aed44dd commit fc6b05c

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

Project 04 - SnapChatMenu/Snapchat Menu/CameraViewController.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010
import AVFoundation
1111

12-
class CameraViewController: UIViewController, UIGestureRecognizerDelegate {
12+
class CameraViewController: UIViewController {
1313

1414
@IBOutlet private weak var cameraView: UIView!
1515
@IBOutlet private weak var tempImageView: UIImageView!
@@ -20,29 +20,42 @@ class CameraViewController: UIViewController, UIGestureRecognizerDelegate {
2020

2121
private var didTakePhoto = false
2222

23+
var showing = false{
24+
willSet {
25+
if newValue == showing {
26+
return
27+
}
28+
if newValue {
29+
captureSession.startRunning()
30+
print("start running")
31+
}else{
32+
captureSession.stopRunning()
33+
print("stop running")
34+
}
35+
}
36+
}
37+
2338
override func viewDidLoad() {
2439
super.viewDidLoad()
2540
configRecognizer()
2641
configCamera()
2742
}
2843

2944
private func configRecognizer() {
30-
let gesture = UITapGestureRecognizer.init(target: self, action: #selector(didPressTakeAnother))
31-
gesture.delegate = self
45+
let gesture = UITapGestureRecognizer.init(target: self, action: #selector(didTouchScreen))
3246
view.addGestureRecognizer(gesture)
3347
}
3448

35-
func didPressTakeAnother() {
49+
func didTouchScreen() {
3650
if !isCameraAvailable() {
3751
return
3852
}
3953
if didTakePhoto {
4054
tempImageView.isHidden = true
4155
didTakePhoto = false
4256
} else {
43-
captureSession.startRunning()
4457
didTakePhoto = true
45-
didPressTakePhoto()
58+
takePhoto()
4659
}
4760
}
4861

@@ -70,10 +83,9 @@ class CameraViewController: UIViewController, UIGestureRecognizerDelegate {
7083
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect
7184
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
7285
cameraView.layer.addSublayer(previewLayer)
73-
captureSession.startRunning()
7486
}
7587

76-
private func didPressTakePhoto() {
88+
private func takePhoto() {
7789
guard let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) else {
7890
return
7991
}

Project 04 - SnapChatMenu/Snapchat Menu/ViewController.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import UIKit
1010

11-
class ViewController: UIViewController {
11+
class ViewController: UIViewController, UIScrollViewDelegate {
1212

1313
@IBOutlet weak var scrollView: UIScrollView!
14+
var cameraViewController: CameraViewController!
1415

1516
override var prefersStatusBarHidden: Bool {
1617
return true
@@ -23,7 +24,7 @@ class ViewController: UIViewController {
2324

2425
private func configScrollView() {
2526
let leftViewController: LeftViewController = LeftViewController(nibName: "LeftViewController", bundle: nil)
26-
let cameraViewController: CameraViewController = CameraViewController(nibName: "CameraViewController", bundle: nil)
27+
cameraViewController = CameraViewController(nibName: "CameraViewController", bundle: nil)
2728
let rightViewController: RightViewController = RightViewController(nibName: "RightViewController", bundle: nil)
2829

2930
addChildViewController(leftViewController)
@@ -43,6 +44,15 @@ class ViewController: UIViewController {
4344
itemFrame.origin.x = 2 * view.frame.width
4445
rightViewController.view.frame = itemFrame
4546
scrollView.contentSize = CGSize(width: view.frame.width * 3, height: view.frame.size.height)
47+
scrollView.delegate = self
48+
}
49+
50+
func scrollViewDidScroll(_ scrollView: UIScrollView) {
51+
let offsetX = scrollView.contentOffset.x
52+
let cameraViewShowing = offsetX > 0 && offsetX < view.frame.width * 2
53+
if cameraViewShowing != cameraViewController.showing {
54+
cameraViewController.showing = cameraViewShowing
55+
}
4656
}
4757

4858
}

0 commit comments

Comments
 (0)