You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
7.6 KiB
225 lines
7.6 KiB
// |
|
// CustomPickerControllers.swift |
|
// FunDoHealth |
|
// |
|
// Created by ecell on 2018/7/16. |
|
// Copyright © 2020年 ecell. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
enum ImagePickerButtonType { |
|
/// 取消 |
|
case cancelCamera |
|
/// 拍照 |
|
case takePhoto |
|
/// 闪关灯 |
|
case flashClick |
|
/// 前后摄像头 |
|
case switchCameras |
|
} |
|
|
|
typealias ImagePickerSelectedBtnClosure = (ImagePickerButtonType) -> Void |
|
class CustomPickerControllers: UIImagePickerController { |
|
|
|
// 记录闪关灯的开关 |
|
var isFlash: Bool = false |
|
var isCanTakePhoto: Bool = false |
|
var isCancelPhoto: Bool = false |
|
weak var flashBtn: UIButton! |
|
weak var switchBtn: UIButton! |
|
weak var cancelBtn: UIButton! |
|
weak var takeBtn: UIButton! |
|
|
|
var selectedClosure: ImagePickerSelectedBtnClosure? |
|
|
|
override func viewDidLoad() { |
|
super.viewDidLoad() |
|
|
|
setupUI() |
|
bindUI() |
|
} |
|
|
|
func setupUI() { |
|
self.modalTransitionStyle = UIModalTransitionStyle.crossDissolve |
|
self.allowsEditing = false |
|
self.showsCameraControls = false |
|
self.isCanTakePhoto = true |
|
self.cameraDevice = UIImagePickerController.CameraDevice.rear |
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.off |
|
|
|
// 自定义拍照界面 - 闪光灯 切换前后摄像头 取消 photograph_btn_icon |
|
var cammeraY: CGFloat = 44.0; |
|
if isIPhoneX { |
|
cammeraY = 84.0; |
|
} |
|
self.cameraViewTransform = CGAffineTransform.init(translationX: 0, y: cammeraY) |
|
// 创建闪关灯按钮 |
|
let flashBtn = UIButton.init(type: UIButton.ButtonType.custom) |
|
flashBtn.frame = CGRect(x: 7, y: isIPhoneX ? 52:12, width: 34, height: 34) |
|
flashBtn.setImage(UIImage(named: "lamplight-close_icon"), for: .normal) |
|
view.addSubview(flashBtn) |
|
self.flashBtn = flashBtn |
|
|
|
// 创建switch_canera_icon按钮 |
|
let switchBtn = UIButton.init(type: UIButton.ButtonType.custom) |
|
switchBtn.frame = CGRect(x: kScreenW - 10 - 34, y: isIPhoneX ? 52:12, width: 34, height: 34) |
|
switchBtn.setImage(UIImage(named: "switch_canera_icon"), for: .normal) |
|
view.addSubview(switchBtn) |
|
self.switchBtn = switchBtn |
|
|
|
var bottomSpacing = kScaleHeight(20.0) |
|
if isIPhone5 || isIPhone4 { |
|
bottomSpacing = kScaleHeight(8.0) |
|
}else if (isIPhoneX) { |
|
bottomSpacing = kScaleHeight(20+34) |
|
} |
|
|
|
// 创建取消按钮 |
|
let cancelBtn = UIButton.init(type: UIButton.ButtonType.custom) |
|
cancelBtn.frame = CGRect(x: 8, y: kScreenH - 50 - bottomSpacing, width: 80, height: 50) |
|
cancelBtn.setTitle(MultiLanguageKey_FB.cancelFB.localized, for: .normal) |
|
view.addSubview(cancelBtn) |
|
self.cancelBtn = cancelBtn |
|
|
|
// 创建拍照 |
|
let takeBtn = UIButton.init(type: UIButton.ButtonType.custom) |
|
takeBtn.frame = CGRect(x: kScreenW/2.0 - 32.5, y: kScreenH - 65 - bottomSpacing, width: 65, height: 65) |
|
takeBtn.setImage(UIImage(named: "photograph_btn_icon"), for: .normal) |
|
view.addSubview(takeBtn) |
|
self.takeBtn = takeBtn |
|
|
|
// 照片标签 |
|
let photoLabel = UILabel.init(frame: CGRect(x: 0, y: takeBtn.mj_y - kScaleHeight(25.0), width: kScreenW, height: 17.0)) |
|
photoLabel.textAlignment = .center |
|
photoLabel.font = SystemRegularFont(15) |
|
photoLabel.text = MultiLanguageKey_FB.photoFB.localized |
|
photoLabel.textColor = UIColor.yellow |
|
view.addSubview(photoLabel) |
|
|
|
} |
|
|
|
func bindUI() { |
|
/** |
|
切换闪光灯模式 |
|
*/ |
|
self.flashBtn.rx.tap |
|
.asObservable().subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
self.flashBtn.setImage(self.loopFlashState(), for: .normal) |
|
if let selectedClosure = self.selectedClosure { |
|
selectedClosure(ImagePickerButtonType.flashClick) |
|
} |
|
}).disposed(by: rx.disposeBag) |
|
/** |
|
切换前后摄像头 |
|
*/ |
|
self.switchBtn.rx.tap.asObservable() |
|
.subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
if self.cameraDevice == UIImagePickerController.CameraDevice.rear { |
|
self.cameraDevice = UIImagePickerController.CameraDevice.front |
|
}else { |
|
self.cameraDevice = UIImagePickerController.CameraDevice.rear |
|
} |
|
|
|
UIView.animate(withDuration: 0.5, animations: { |
|
self.cameraViewTransform = CGAffineTransform.init(scaleX: -1.0, y: 1.0) |
|
}) |
|
|
|
if let selectedClosure = self.selectedClosure { |
|
selectedClosure(ImagePickerButtonType.switchCameras) |
|
} |
|
}).disposed(by: rx.disposeBag) |
|
|
|
/** |
|
关闭相机 |
|
*/ |
|
self.cancelBtn.rx.tap.asObservable() |
|
.subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
guard self.isCanTakePhoto else { return } |
|
self.isCancelPhoto = false |
|
if let selectedClosure = self.selectedClosure { |
|
selectedClosure(ImagePickerButtonType.cancelCamera) |
|
} |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
|
|
/** |
|
拍照 |
|
*/ |
|
self.takeBtn.rx.tap.asObservable() |
|
.subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
self.cancelAble() |
|
self.takePicture() |
|
if let selectedClosure = self.selectedClosure { |
|
selectedClosure(ImagePickerButtonType.takePhoto) |
|
} |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
|
|
} |
|
|
|
override func viewWillAppear(_ animated: Bool) { |
|
super.viewWillAppear(animated) |
|
|
|
// 防止默认每次进去默认lamplight-close_icon的问题 |
|
if isFlash { |
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.on |
|
} |
|
self.isCancelPhoto = true |
|
} |
|
|
|
|
|
/// 拍照 |
|
func selfTakePicture() { |
|
if isFlash { |
|
guard self.isCanTakePhoto else { return } |
|
self.isCanTakePhoto = false |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { [weak self] in |
|
guard let `self` = self else { return } |
|
self.isCanTakePhoto = true |
|
} |
|
} |
|
|
|
if self.isCancelPhoto { |
|
self.takePicture() |
|
} |
|
} |
|
} |
|
|
|
extension CustomPickerControllers { |
|
|
|
/// 循环闪光灯状态切换 |
|
/// |
|
/// - Returns: 闪关按钮图片 |
|
func loopFlashState() -> UIImage { |
|
isFlash = !isFlash |
|
var imageStr: String = "" |
|
if isFlash { |
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.on |
|
imageStr = "lamplight-open_icon" |
|
}else { |
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.off |
|
imageStr = "lamplight-close_icon" |
|
} |
|
return UIImage(named: imageStr)! |
|
} |
|
|
|
//退出按钮1秒后可以点击 |
|
func cancelAble() { |
|
var seconds: Double = 1.0 |
|
if self.cameraFlashMode == UIImagePickerController.CameraFlashMode.on { |
|
seconds = 5.0 |
|
} |
|
self.view.window?.isUserInteractionEnabled = false |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) { [weak self] in |
|
guard let `self` = self else { return } |
|
self.view.window?.isUserInteractionEnabled = true |
|
} |
|
} |
|
}
|
|
|