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.
226 lines
7.6 KiB
226 lines
7.6 KiB
![]()
2 years ago
|
//
|
||
![]()
2 years ago
|
// CustomPickerControllers.swift
|
||
![]()
2 years ago
|
// 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
|
||
![]()
2 years ago
|
class CustomPickerControllers: UIImagePickerController {
|
||
![]()
2 years ago
|
|
||
|
// 记录闪关灯的开关
|
||
|
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
|
||
|
|
||
![]()
1 year ago
|
// 自定义拍照界面 - 闪光灯 切换前后摄像头 取消 photograph_btn_icon
|
||
![]()
2 years ago
|
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)
|
||
![]()
1 year ago
|
flashBtn.setImage(UIImage(named: "lamplight-close_icon"), for: .normal)
|
||
![]()
2 years ago
|
view.addSubview(flashBtn)
|
||
|
self.flashBtn = flashBtn
|
||
|
|
||
![]()
1 year ago
|
// 创建switch_canera_icon按钮
|
||
![]()
2 years ago
|
let switchBtn = UIButton.init(type: UIButton.ButtonType.custom)
|
||
|
switchBtn.frame = CGRect(x: kScreenW - 10 - 34, y: isIPhoneX ? 52:12, width: 34, height: 34)
|
||
![]()
1 year ago
|
switchBtn.setImage(UIImage(named: "switch_canera_icon"), for: .normal)
|
||
![]()
2 years ago
|
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)
|
||
![]()
1 year ago
|
cancelBtn.setTitle(MultiLanguageKey_FB.cancelFB.localized, for: .normal)
|
||
![]()
2 years ago
|
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)
|
||
![]()
1 year ago
|
takeBtn.setImage(UIImage(named: "photograph_btn_icon"), for: .normal)
|
||
![]()
2 years ago
|
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)
|
||
![]()
1 year ago
|
photoLabel.text = MultiLanguageKey_FB.photoFB.localized
|
||
![]()
2 years ago
|
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)
|
||
|
|
||
![]()
1 year ago
|
// 防止默认每次进去默认lamplight-close_icon的问题
|
||
![]()
2 years ago
|
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()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
2 years ago
|
extension CustomPickerControllers {
|
||
![]()
2 years ago
|
|
||
|
/// 循环闪光灯状态切换
|
||
|
///
|
||
|
/// - Returns: 闪关按钮图片
|
||
|
func loopFlashState() -> UIImage {
|
||
|
isFlash = !isFlash
|
||
|
var imageStr: String = ""
|
||
|
if isFlash {
|
||
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.on
|
||
![]()
1 year ago
|
imageStr = "lamplight-open_icon"
|
||
![]()
2 years ago
|
}else {
|
||
|
self.cameraFlashMode = UIImagePickerController.CameraFlashMode.off
|
||
![]()
1 year ago
|
imageStr = "lamplight-close_icon"
|
||
![]()
2 years ago
|
}
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|