全部产品

进阶指南

更新时间:2020-09-14 15:08:41

本文将结合 扫一扫 官方 Demo 介绍如何在 10.1.60 及以上版本的基线中使用默认 UI 下的扫码功能和自定义 UI 下的扫码功能。

默认 UI 下使用扫一扫

在默认 UI 下修改扫码所在页面的参数。

  1. ```objectivec
  2. - (void)custoDefaultScan {
  3. TBScanViewController *vc = [[MPScanCodeAdapterInterface sharedInstance] createDefaultScanPageWithallback:^(id _Nonnull result, BOOL keepAlive) {
  4. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:result[@"resp_result"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  5. alert.tag = 1001;
  6. [alert show];
  7. }];
  8. [self.navigationController pushViewController:vc animated:YES];
  9. self.scanVC = vc;
  10. // 设置扫码界面 title
  11. vc.title = @"标准扫码";
  12. // 设置打开手电筒提示文字
  13. vc.torchStateNormalTitle = @"打开手电筒";
  14. // 设置关闭手电筒提示文字
  15. vc.torchStateSelectedTitle = @"关闭手电筒";
  16. // 设置扫码识别类型
  17. vc.scanType = ScanType_QRCode;
  18. // 设置选择相册按钮
  19. vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:APCommonUILoadImage(@"camera") style:UIBarButtonItemStylePlain target:self action:@selector(selectPhotos)];
  20. }
  21. - (void)selectPhotos
  22. {
  23. [self.scanVC scanPhotoLibrary];
  24. }
  25. ```

自定义 UI 下使用扫一扫

若您需要完全自定义扫码 UI 界面,可自定义扫码页并让其继承 TBScanViewController。

  • 创建扫码页,并自定义扫码区。

    1. @interface MPScanCodeViewController : TBScanViewController <TBScanViewControllerDelegate>
    2. @end
    3. @implementation MPScanCodeViewController
    4. - (instancetype)init
    5. {
    6. if (self = [super init])
    7. {
    8. self.delegate = self;
    9. self.scanType = ScanType_All_Code;
    10. }
    11. return self;
    12. }
    13. - (void)viewDidLoad {
    14. [super viewDidLoad];
    15. // Do any additional setup after loading the view.
    16. self.title = @"扫码";
    17. // 自定义扫码界面大小
    18. CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
    19. self.rectOfInterest = rect;
    20. // 自定义相册按钮
    21. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"选择相册" style:UIBarButtonItemStylePlain target:self action:@selector(selectPhoto)];
    22. }
    23. + (CGRect)constructScanAnimationRect
    24. {
    25. CGSize screenXY = [UIScreen mainScreen].bounds.size;
    26. NSInteger focusFrameWH = screenXY.width / 320 * 220;//as wx
    27. int offet = 10;
    28. if (screenXY.height == 568)
    29. offet = 19;
    30. return CGRectMake((screenXY.width - focusFrameWH) / 2,
    31. (screenXY.height - 64 - focusFrameWH - 83 - 50 - offet) / 2 + 64,
    32. focusFrameWH,
    33. focusFrameWH);
    34. }
    35. -(void)buildContainerView:(UIView*)containerView
    36. {
    37. // 自定义扫码框 view
    38. UIView* bg = [[UIView alloc] initWithFrame:containerView.bounds];
    39. [containerView addSubview:bg];
    40. CGRect rect = [MPScanCodeViewController constructScanAnimationRect];
    41. UIView* view = [[UIView alloc] initWithFrame:rect];
    42. view.backgroundColor = [UIColor orangeColor];
    43. view.alpha = 0.5;
    44. [bg addSubview:view];
    45. }
    46. - (void)selectPhoto
    47. {
    48. [self scanPhotoLibrary];
    49. }
  • 处理扫码结果。

    1. -(void)didFind:(NSArray<TBScanResult*>*)resultArray
    2. {
    3. TBScanResult *result = resultArray.firstObject;
    4. NSString* content = result.data;
    5. if (result.resultType == TBScanResultTypeQRCode) {
    6. content = [NSString stringWithFormat:@"qrcode:%@, hiddenData:%@, TBScanQRCodeResultType:%@", result.data, result.hiddenData, [result.extData objectForKey:TBScanResultTypeQRCode]];
    7. NSLog(@"subType is %@, ScanType_QRCode is %@", @(result.subType), @(ScanType_QRCode));
    8. } else if (result.resultType == TBScanResultTypeVLGen3Code) {
    9. content = [NSString stringWithFormat:@"gen3:%@", result.data];
    10. NSLog(@"subType is %@, ScanType_GEN3 is %@", @(result.subType), @(ScanType_GEN3));
    11. } else if (result.resultType == TBScanResultTypeGoodsBarcode) {
    12. content = [NSString stringWithFormat:@"barcode:%@", result.data];
    13. NSLog(@"subType is %@, EAN13 is %@", @(result.subType), @(EAN13));
    14. } else if (result.resultType == TBScanResultTypeDataMatrixCode) {
    15. content = [NSString stringWithFormat:@"dm:%@", result.data];
    16. NSLog(@"subType is %@, ScanType_DATAMATRIX is %@", @(result.subType), @(ScanType_DATAMATRIX));
    17. } else if (result.resultType == TBScanResultTypeExpressCode) {
    18. content = [NSString stringWithFormat:@"express:%@", result.data];
    19. NSLog(@"subType is %@, ScanType_FASTMAIL is %@", @(result.subType), @(ScanType_FASTMAIL));
    20. }
    21. dispatch_async(dispatch_get_main_queue(), ^{
    22. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:content delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    23. alert.tag = 9999;
    24. [alert show];
    25. });
    26. }
  • 持续扫码。

    1. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    2. // 持续扫码
    3. [self resumeScan];
    4. }
  • 本地相册识别失败的回调。
    1. - (void)scanPhotoFailed
    2. {
    3. // 相册识别失败的回调
    4. NSLog(@"scanPhotoFailed");
    5. }
  • 其他回调处理。

    1. - (void)cameraPermissionDenied
    2. {
    3. [self.navigationController popViewControllerAnimated:YES];
    4. }
    5. - (void)cameraDidStart
    6. {
    7. NSLog(@"started!!");
    8. }
    9. -(void)setTorchState:(TorchState)bState
    10. {
    11. NSLog(@"TorchState:%lu", (unsigned long)bState);
    12. }
    13. -(void)userTrack:(NSString*)name
    14. {
    15. NSLog(@"userTrack:%@", name);
    16. }
    17. -(void)userTrack:(NSString*)name args:(NSDictionary*)data
    18. {
    19. NSLog(@"userTrack:%@, args:%@", name, data);
    20. }
    21. - (void)scanPhotoFailed
    22. {
    23. // 相册识别失败的回调
    24. NSLog(@"scanPhotoFailed");
    25. }