#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
#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{
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];
}
- (
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];
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];
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];
}
-(
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