自定义viewController的基础类

xiaoxiao2021-02-28  9

// // LJBaseViewController.h // TyApp // // Created by TY-DENG on 16/11/24. // Copyright © 2016年 Lin. All rights reserved. // #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <UIKit+AFNetworking.h> #import "LJTool.h" #import "LJNetWorkUrl.h" typedef void (^NetWorkReturnDictionary)(NSDictionary *dictionary); @interface LJBaseViewController : UIViewController<UITextFieldDelegate> { AVCaptureSession *CaptureSession;//扫描 UIView *CaptureVideoPreviewLayerView; //扫描 UIButton *doneInKeyboardButton;//数据键回收键盘 } //设置网络 -(void) netWorkWithString:(NSString *)string WithKey:(NSInteger )key HandlerBlock:(NetWorkReturnDictionary) block; //开始扫描 -(void) scanActionStartDelegate:(id<AVCaptureMetadataOutputObjectsDelegate>)delegate ; //停止扫描 -(void)scanActionStopCaptureSession; -(void) viewAnimations:(CGRect)bounds Duration:(CGFloat)time View:(UIView *)view; -(void) naviagtionControllerBarButtonItemBack; -(void) backViewcontroller; @end // // LJBaseViewController.m // TyApp // // Created by TY-DENG on 16/11/24. // Copyright © 2016年 Lin. All rights reserved. // #import "LJBaseViewController.h" @interface LJBaseViewController () { UITextField *_EditTextField; } @end @implementation LJBaseViewController - (void)viewDidLoad { [super viewDidLoad]; [self NoitifinationCenterListener]; [self setNavgation]; } -(void) setNavgation{ self.navigationItem.hidesBackButton = YES;//可以隐藏原有的导航栏返回按钮 //设置的按钮会显示在导航栏左边,返回按钮的位置 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"activity_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewcontroller)]; self.navigationItem.leftBarButtonItem = barButtonItem; UINavigationBar *bar = [UINavigationBar appearance]; //设置显示的颜色 bar.barTintColor = [UIColor clearColor]; //设置字体颜色 bar.tintColor = [UIColor whiteColor]; [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; } /** 扫描二维码事件 **/ //开始扫描 -(void) scanActionStartDelegate:(id<AVCaptureMetadataOutputObjectsDelegate>)delegate { //判断摄像头情况 NSString * mediaType = AVMediaTypeVideo; AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) { [LJTool setAlertViewControllerTittle:@"提示" Message:@"摄像头获取受限!请检查摄像头设置。" Button:@"我知道了" handlerBlock:^(UIAlertAction *action) { } viewController:self]; }else{ // Do any additional setup after loading the view, typically from a nib. //获取摄像设备 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //创建输入流 AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //创建输出流 AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; //设置代理 在主线程里刷新 [output setMetadataObjectsDelegate:delegate queue:dispatch_get_main_queue()]; //初始化链接对象 CaptureSession = [[AVCaptureSession alloc]init]; //高质量采集率 [CaptureSession setSessionPreset:AVCaptureSessionPresetHigh]; if(input!= nil){ [CaptureSession addInput:input]; [CaptureSession addOutput:output]; //设置扫码支持的编码格式(如下设置条形码和二维码兼容) output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:CaptureSession]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; [self setAVCaptureVideoPreviewLayerViewWithAVCaptureSession:layer]; //开始捕获 [CaptureSession startRunning]; }else{ [LJTool setAlertViewControllerTittle:@"提示" Message:@"获取摄像头出了点问题,请检查权限!" Button:@"我知道了" handlerBlock:^(UIAlertAction *action) { } viewController:self]; } } } -(void) setAVCaptureVideoPreviewLayerViewWithAVCaptureSession:(CALayer*) avSession { CaptureVideoPreviewLayerView = [[UIView alloc] initWithFrame:CGRectMake(20, 164, self.view.frame.size.width - 40,200)]; UIButton *stopButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, CaptureVideoPreviewLayerView.frame.size.width, 50)]; [stopButton setTitle:@"关闭扫描" forState:UIControlStateNormal]; [stopButton addTarget:self action:@selector(scanActionStopCaptureSession) forControlEvents:UIControlEventTouchUpInside]; [stopButton setBackgroundColor:[UIColor grayColor]]; UIView *PreviewLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CaptureVideoPreviewLayerView.frame.size.width,150)]; [CaptureVideoPreviewLayerView addSubview:PreviewLayerView]; [CaptureVideoPreviewLayerView addSubview:stopButton]; [self.view addSubview:CaptureVideoPreviewLayerView]; avSession.frame= PreviewLayerView.bounds; [PreviewLayerView.layer insertSublayer:avSession above:0]; } -(void)scanActionStopCaptureSession{ [CaptureSession stopRunning]; [CaptureVideoPreviewLayerView removeFromSuperview]; } /* 防止UITextField被键盘遮盖 */ - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return true; } -(void) NoitifinationCenterListener{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil]; } -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField { _EditTextField = textField; return YES; } -(void) KeyboardWillShow:(NSNotification *)notification { if(_EditTextField != nil){ NSDictionary *userInfo = notification.userInfo; CGRect KeyboardFrameEndUserInfoKey = [[userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; CGFloat marginBottom = [UIScreen mainScreen].bounds.size.height - _EditTextField.frame.size.height - _EditTextField.frame.origin.y - 5; CGFloat margin = marginBottom - KeyboardFrameEndUserInfoKey.size.height; if(margin <0){ [self textFieldAnimations:-margin]; } } } -(void) keyboardWillHidden:(NSNotification *)notification { _EditTextField = nil; [self textFieldAnimations:0]; } -(void) textFieldAnimations:(CGFloat )move { CGRect frame = self.view.frame; //首尾式动画 [UIView beginAnimations:nil context:nil]; //执行动画 //设置动画执行时间 [UIView setAnimationDuration:0.25]; //设置代理 [UIView setAnimationDelegate:self]; //设置动画执行完毕调用的事件 // [UIView setAnimationDidStopSelector:@selector(didStopAnimation)]; self.view.bounds = CGRectMake(0,move, frame.size.width, frame.size.height) ; [UIView commitAnimations]; } -(void) viewAnimations:(CGRect)bounds Duration:(CGFloat)time View:(UIView *)view{ //首尾式动画 [UIView beginAnimations:nil context:nil]; //执行动画 //设置动画执行时间 [UIView setAnimationDuration:time]; //设置代理 [UIView setAnimationDelegate:self]; //设置动画执行完毕调用的事件 // [UIView setAnimationDidStopSelector:@selector(didStopAnimation)]; view.bounds = bounds ; [UIView commitAnimations]; } //设置网络 -(void) netWorkWithString:(NSString *)string WithKey:(NSInteger )key HandlerBlock:(NetWorkReturnDictionary) block { string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; NSString * encodingString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@",encodingString); AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; session.responseSerializer = [AFHTTPResponseSerializer serializer]; session.requestSerializer.timeoutInterval = 30; [session GET:encodingString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil]; block(dic); if (![[dic objectForKey:@"Status"] boolValue]) { if(key == 1){ NSString *msg; if ([LJTool isEmptyNilNullLength0:[dic objectForKey:@"Msg"]]) { msg = @"操作失败!"; }else{ msg = [[dic objectForKey:@"Msg"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } [LJTool setAlertViewControllerTittle:@"提示" Message:msg Button:@"我知道了" handlerBlock:^(UIAlertAction *action ) { } viewController:self]; } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [LJTool setAlertViewControllerTittle:@"提示" Message:@"网络连接失败,请检查网络!" Button:@"我知道了" handlerBlock:^(UIAlertAction *action) { } viewController:self]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; } -(void) naviagtionControllerBarButtonItemBack{ UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"activity_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewcontroller)]; [leftItem setBackgroundImage:[UIImage imageNamed:@"activity_back.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; self.navigationItem.backBarButtonItem = leftItem; } -(void) backViewcontroller{ [self.navigationController popViewControllerAnimated:YES]; } @end
转载请注明原文地址: https://www.6miu.com/read-450209.html

最新回复(0)