123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- //
- // WCQRCodeVC.m
- // SGQRCodeExample
- //
- // Created by kingsic on 17/3/20.
- // Copyright © 2017年 kingsic. All rights reserved.
- //
- #import "WCQRCodeVC.h"
- #import "SGQRCode.h"
- #import "ScanSuccessJumpVC.h"
- #import "MBProgressHUD+SGQRCode.h"
- @interface WCQRCodeVC () {
- SGQRCodeObtain *obtain;
- }
- @property (nonatomic, strong) SGQRCodeScanView *scanView;
- @property (nonatomic, strong) UIButton *flashlightBtn;
- @property (nonatomic, strong) UILabel *promptLabel;
- @property (nonatomic, assign) BOOL isSelectedFlashlightBtn;
- @property (nonatomic, strong) UIView *bottomView;
- @property(nonatomic,strong)UILabel *titleLab;
- @end
- @implementation WCQRCodeVC
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- /// 二维码开启方法
- [obtain startRunningWithBefore:nil completion:nil];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- [self.scanView addTimer];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [self.scanView removeTimer];
- [self removeFlashlightBtn];
- [obtain stopRunning];
- }
- - (void)dealloc {
- NSLog(@"WCQRCodeVC - dealloc");
- [self removeScanningView];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- self.view.backgroundColor = [UIColor blackColor];
- [self.navigationView removeAllLeftButton];
-
- _titleLab = [[UILabel alloc]init];
-
- _titleLab.frame = CGRectMake(0,0, kGScreenWidth-60, 30);
-
- _titleLab.text = @"扫码签到";
-
- [self.navigationView addTitleView:_titleLab];
-
- kWeakSelf(self);
- [self.navigationView addLeftButtonWithImage:[UIImage imageNamed:@"btn_back"] clickCallBack:^(UIView *view) {
-
- [weakself.navigationController popViewControllerAnimated:YES];
-
- }];
- obtain = [SGQRCodeObtain QRCodeObtain];
-
- [self setupQRCodeScan];
- [self setupNavigationBar];
- [self.view addSubview:self.scanView];
- [self.view addSubview:self.promptLabel];
- /// 为了 UI 效果
- [self.view addSubview:self.bottomView];
- }
- - (void)setupQRCodeScan {
- __weak typeof(self) weakSelf = self;
-
- SGQRCodeObtainConfigure *configure = [SGQRCodeObtainConfigure QRCodeObtainConfigure];
- configure.sampleBufferDelegate = YES;
- [obtain establishQRCodeObtainScanWithController:self configure:configure];
- [obtain setBlockWithQRCodeObtainScanResult:^(SGQRCodeObtain *obtain, NSString *result) {
- if (result) {
- [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view];
- [obtain stopRunning];
- [obtain playSoundName:@"SGQRCode.bundle/sound.caf"];
-
- ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
- jumpVC.comeFromVC = ScanSuccessJumpComeFromWC;
- jumpVC.jump_URL = result;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [MBProgressHUD SG_hideHUDForView:weakSelf.view];
- [weakSelf.navigationController pushViewController:jumpVC animated:YES];
- });
- }
- }];
- [obtain setBlockWithQRCodeObtainScanBrightness:^(SGQRCodeObtain *obtain, CGFloat brightness) {
- if (brightness < - 1) {
- [weakSelf.view addSubview:weakSelf.flashlightBtn];
- } else {
- if (weakSelf.isSelectedFlashlightBtn == NO) {
- [weakSelf removeFlashlightBtn];
- }
- }
- }];
- }
- - (void)setupNavigationBar {
- self.navigationItem.title = @"扫一扫";
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItenAction)];
- }
- - (void)rightBarButtonItenAction {
- __weak typeof(self) weakSelf = self;
-
- [obtain establishAuthorizationQRCodeObtainAlbumWithController:nil];
- if (obtain.isPHAuthorization == YES) {
- [self.scanView removeTimer];
- }
- [obtain setBlockWithQRCodeObtainAlbumDidCancelImagePickerController:^(SGQRCodeObtain *obtain) {
- [weakSelf.view addSubview:weakSelf.scanView];
- }];
- [obtain setBlockWithQRCodeObtainAlbumResult:^(SGQRCodeObtain *obtain, NSString *result) {
- [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view];
- if (result == nil) {
- NSLog(@"暂未识别出二维码");
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [MBProgressHUD SG_hideHUDForView:weakSelf.view];
- [MBProgressHUD SG_showMBProgressHUDWithOnlyMessage:@"未发现二维码/条形码" delayTime:1.0];
- });
- } else {
- ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
- jumpVC.comeFromVC = ScanSuccessJumpComeFromWC;
- if ([result hasPrefix:@"http"]) {
- jumpVC.jump_URL = result;
- } else {
- jumpVC.jump_bar_code = result;
- }
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [MBProgressHUD SG_hideHUDForView:weakSelf.view];
- [weakSelf.navigationController pushViewController:jumpVC animated:YES];
- });
- }
- }];
- }
- - (SGQRCodeScanView *)scanView {
- if (!_scanView) {
- _scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.9 * self.view.frame.size.height)];
- }
- return _scanView;
- }
- - (void)removeScanningView {
- [self.scanView removeTimer];
- [self.scanView removeFromSuperview];
- self.scanView = nil;
- }
- - (UILabel *)promptLabel {
- if (!_promptLabel) {
- _promptLabel = [[UILabel alloc] init];
- _promptLabel.backgroundColor = [UIColor clearColor];
- CGFloat promptLabelX = 0;
- CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
- CGFloat promptLabelW = self.view.frame.size.width;
- CGFloat promptLabelH = 25;
- _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
- _promptLabel.textAlignment = NSTextAlignmentCenter;
- _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
- _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
- _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
- }
- return _promptLabel;
- }
- - (UIView *)bottomView {
- if (!_bottomView) {
- _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scanView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(self.scanView.frame))];
- _bottomView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
- }
- return _bottomView;
- }
- #pragma mark - - - 闪光灯按钮
- - (UIButton *)flashlightBtn {
- if (!_flashlightBtn) {
- // 添加闪光灯按钮
- _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
- CGFloat flashlightBtnW = 30;
- CGFloat flashlightBtnH = 30;
- CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW);
- CGFloat flashlightBtnY = 0.55 * self.view.frame.size.height;
- _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH);
- [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightOpenImage"] forState:(UIControlStateNormal)];
- [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightCloseImage"] forState:(UIControlStateSelected)];
- [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _flashlightBtn;
- }
- - (void)flashlightBtn_action:(UIButton *)button {
- if (button.selected == NO) {
- [obtain openFlashlight];
- self.isSelectedFlashlightBtn = YES;
- button.selected = YES;
- } else {
- [self removeFlashlightBtn];
- }
- }
- - (void)removeFlashlightBtn {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [obtain closeFlashlight];
- self.isSelectedFlashlightBtn = NO;
- self.flashlightBtn.selected = NO;
- [self.flashlightBtn removeFromSuperview];
- });
- }
- @end
|