相机检测

//相机相关的检测方法
-(BOOL)checkCameraAbout:(BOOL)isAlterBox{
    
    //是否开启相机
    if (![self checkCamera:isAlterBox]) {
        
        return NO;
        
    }else{
        
        //相机权限检测 02
        if (![self checkCameraPermission:isAlterBox]) {

            return NO;
            
        }else{
            
            return YES;
        }
    }
}


/**
 *  是否有相机检测 01
 *
 *  @param isAlterBox 是否页面提示
 *
 *  @return YES & NO(无相机)
 */
-(BOOL)checkCamera:(BOOL)isAlterBox{
    
    AVCaptureDevice *devece = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (devece == nil) {
        
        self.view.alpha = 1.0f;
        
        //提示,未检测到相机
        if (isAlterBox) {
            
            [self alterBoxTitle:@"未检测到相机" message:@"请去设置-通用-访问限制中开启相机" mask:0 idObj:nil];
        }
        
        return NO;
        
    }else{
        
        return YES;
    }

}


/**
 *  对是否拥有相机权限进行检测 02
 *
 *  @param isAlterBox 是否有提示
 *
 *  @return YES & NO ( no 没有权限)
 */
-(BOOL)checkCameraPermission:(BOOL)isAlterBox{
    
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        //弹框警告,没有授权相机
        if (isAlterBox) {
            
            [self alterBoxTitle:@"没有相机权限" message:@"请去设置-隐私-相机中对证联钱包授权" mask:0 idObj:nil];
        }
        
        return NO;
        
    }else{
        
        return YES;
    }
}
View Code 相关代码,其中相关代码使用到下面的参考

 

#import <UIKit/UIKit.h>
typedef enum ButtonType{
    Button_OK,
    Button_CANCEL,
    Button_OTHER
}ButtonType;

@class ZLAlertDialogItem;
typedef void(^ZLAlertDialogHandler)(ZLAlertDialogItem *item);

@interface ZLAlertDialog : UIView
{
    UIView *_coverView;
    UIView *_alertView;
    UILabel *_labelTitle;
    UILabel *_labelmessage;
    
    UIScrollView *_buttonScrollView;
    UIScrollView *_contentScrollView;
    
    NSMutableArray *_items;
    NSString *_title;
    NSString *_messaege;
}

//按钮宽度,如果赋值,菜单按钮宽之和,超过alert宽,菜单会滚动
@property(assign,nonatomic)CGFloat buttonWidth;
//将要显示在alert上的自定义view
@property(strong,nonatomic)UIView *contentView;

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message;
- (void)addButton:(ButtonType)type withTitle:(NSString *)title handler:(ZLAlertDialogHandler)handler;
- (void)show;
- (void)dismiss;

@end

@interface ZLAlertDialogItem : NSObject
@property(nonatomic,copy) NSString *title;
@property(nonatomic) ButtonType type;
@property(nonatomic) NSUInteger tag;
@property(nonatomic, copy) ZLAlertDialogHandler action;

@end
View Code  要使用到的弹框三方类 .h
#define AlertPadding 10
#define MenuHeight   44

#define AlertHeight  130 //提示框的宽度
#define AlertWidth   270 //提示框的高度

#import "ZLAlertDialog.h"

@implementation ZLAlertDialogItem
@end

@implementation ZLAlertDialog
- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message{
    self = [super init];
    if (self) {
        _items = [[NSMutableArray alloc] init];
        _title  = title;
        _messaege = message;
        
        [self buildViews];
    }
    return self;
}
-(void)buildViews{
    self.frame = [self screenBounds];//自定义大小
    _coverView = [[UIView alloc]initWithFrame:[self topView].bounds];
    _coverView.alpha = 0.5;//提示框下面的视图的透明度渲染
    _coverView.backgroundColor = [UIColor blackColor];//在点击相应的时候,提示框下面的视图的颜色渲染
    _coverView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [[self topView] addSubview:_coverView];
    
    _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, AlertWidth, AlertHeight)];
    _alertView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    _alertView.layer.cornerRadius = 5;
    _alertView.layer.masksToBounds = YES;
    _alertView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:_alertView];
    
    //title
    CGFloat labelHeigh = [self heightWithString:_title fontSize:17 width:AlertWidth-2*AlertPadding];
    _labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(AlertPadding, AlertPadding, AlertWidth-2*AlertPadding, labelHeigh)];
    _labelTitle.font = [UIFont boldSystemFontOfSize:17];
    _labelTitle.textColor = [UIColor blackColor];
    _labelTitle.textAlignment = NSTextAlignmentCenter;
    _labelTitle.numberOfLines = 0;
    _labelTitle.text = _title;
    _labelTitle.lineBreakMode = NSLineBreakByCharWrapping;
    [_alertView addSubview:_labelTitle];
    
    //message
    CGFloat messageHeigh = [self heightWithString:_messaege fontSize:14 width:AlertWidth-2*AlertPadding];
    
    _labelmessage =  [[UILabel alloc]initWithFrame:CGRectMake(AlertPadding, _labelTitle.frame.origin.y+_labelTitle.frame.size.height, AlertWidth-2*AlertPadding, messageHeigh+2*AlertPadding)];
    _labelmessage.font = [UIFont systemFontOfSize:14];
    _labelmessage.textColor = [UIColor blackColor];
    _labelmessage.textAlignment = NSTextAlignmentCenter;
    _labelmessage.text = _messaege;
    _labelmessage.numberOfLines = 0;
    _labelmessage.lineBreakMode = NSLineBreakByCharWrapping;
    [_alertView addSubview:_labelmessage];
    
    
    _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    [_alertView addSubview:_contentScrollView];
    
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    
}
- (void)dealloc
{
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)layoutSubviews{
    _buttonScrollView.frame = CGRectMake(0, _alertView.frame.size.height-MenuHeight,_alertView.frame.size.width, MenuHeight);
    _contentScrollView.frame = CGRectMake(0, _labelTitle.frame.origin.y+_labelTitle.frame.size.height, _alertView.frame.size.width, _alertView.frame.size.height-MenuHeight);
    self.contentView.frame = CGRectMake(0,0,self.contentView.frame.size.width, self.contentView.frame.size.height);
    _contentScrollView.contentSize = self.contentView.frame.size;
    
}
-(void)willMoveToSuperview:(UIView *)newSuperview
{
    [self addButtonItem];
    [_contentScrollView addSubview:self.contentView];
    [self reLayout];
}
-(void)reLayout{
    CGFloat plus;
    if (self.contentView) {
        plus = self.contentView.frame.size.height-(_alertView.frame.size.height-MenuHeight);
    }else{
        plus = _labelmessage.frame.origin.y+_labelmessage.frame.size.height -(_alertView.frame.size.height-MenuHeight);
    }
    plus = MAX(0, plus);
    CGFloat height =  MIN([self screenBounds].size.height-MenuHeight,_alertView.frame.size.height+plus);
    
    _alertView.frame = CGRectMake(_alertView.frame.origin.x, _alertView.frame.origin.y, AlertWidth, height);
    _alertView.center = self.center;
    [self setNeedsDisplay];
    [self setNeedsLayout];
}
- (CGFloat)heightWithString:(NSString*)string fontSize:(CGFloat)fontSize width:(CGFloat)width
{
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    return  [string boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.height;
}
#pragma mark - add item

- (NSInteger)addButtonWithTitle:(NSString *)title{
    ZLAlertDialogItem *item = [[ZLAlertDialogItem alloc] init];
    item.title = title;
    item.action =  ^(ZLAlertDialogItem *item) {
        NSLog(@"no action");
    };
    item.type = Button_OK;
    [_items addObject:item];
    return [_items indexOfObject:title];
}
- (void)addButton:(ButtonType)type withTitle:(NSString *)title handler:(ZLAlertDialogHandler)handler{
    ZLAlertDialogItem *item = [[ZLAlertDialogItem alloc] init];
    item.title = title;
    item.action = handler;
    item.type = type;
    [_items addObject:item];
    item.tag = [_items indexOfObject:item];
}
- (void)addButtonItem {
    _buttonScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _alertView.frame.size.height- MenuHeight,AlertWidth, MenuHeight)];
    _buttonScrollView.bounces = NO;
    _buttonScrollView.showsHorizontalScrollIndicator = NO;
    _buttonScrollView.showsVerticalScrollIndicator =  NO;
    CGFloat  width;
    if(self.buttonWidth){
        width = self.buttonWidth;
        _buttonScrollView.contentSize = CGSizeMake(width*[_items count], MenuHeight);
    }else
    {
        width = _alertView.frame.size.width/[_items count];
    }
    [_items enumerateObjectsUsingBlock:^(ZLAlertDialogItem *item, NSUInteger idx, BOOL *stop) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        //        button.translatesAutoresizingMaskIntoConstraints = NO;
        button.frame = CGRectMake(idx*width, 1, width, MenuHeight);
        //seperator
        button.backgroundColor = [UIColor whiteColor];
        button.layer.shadowColor = [[UIColor grayColor] CGColor];
        button.layer.shadowRadius = 0.5;
        button.layer.shadowOpacity = 1;
        button.layer.shadowOffset = CGSizeZero;
        button.layer.masksToBounds = NO;
        button.tag = 90000+ idx;
        // title
        [button setTitle:item.title forState:UIControlStateNormal];
        [button setTitle:item.title forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:button.titleLabel.font.pointSize];
        
        // action
        [button addTarget:self
                   action:@selector(buttonTouched:)
         forControlEvents:UIControlEventTouchUpInside];
        
        [_buttonScrollView addSubview:button];
    }];
    [_alertView addSubview:_buttonScrollView];
    
}

- (void)buttonTouched:(UIButton*)button{
    ZLAlertDialogItem *item = _items[button.tag-90000];
    if (item.action) {
        item.action(item);
    }
    [self dismiss];
}
#pragma mark - show and dismiss
-(UIView*)topView{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    return  window.subviews[0];
}
- (void)show {
    [UIView animateWithDuration:0.5 animations:^{
        _coverView.alpha = 0.5;
        
    } completion:^(BOOL finished) {
        
    }];
    
    [[self topView] addSubview:self];
    [self showAnimation];
}

- (void)dismiss {
    [self hideAnimation];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//        [self removeFromSuperview];
}

- (void)showAnimation {
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.4;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [_alertView.layer addAnimation:popAnimation forKey:nil];
}

- (void)hideAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        _coverView.alpha = 0.0;
        _alertView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - Handle device orientation changes
// Handle device orientation changes
- (void)deviceOrientationDidChange: (NSNotification *)notification
{
    //    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    self.frame = [self screenBounds];
    //NSLog(@"self.frame%@",NSStringFromCGRect(self.frame));
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                     animations:^{
                         [self reLayout];
                     }
                     completion:nil
     ];
    
    
}
- (CGRect)screenBounds
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    // On iOS7, screen width and height doesn't automatically follow orientation
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
            CGFloat tmp = screenWidth;
            screenWidth = screenHeight;
            screenHeight = tmp;
        }
    }
    
    return CGRectMake(0, 0, screenWidth, screenHeight);
}
@end
View Code  要使用到的弹框三方类 .m
{
    ZLAlertDialog * alterBox;
}

/**
 *  弹出扫描的内容在弹框中提示
 *
 *  对于 maskNum 的说明:0 就是默认功能仅仅有一个OK按钮  // 1、
 *
 *  @param title    提示框标题
 *  @param mesage   提示内容
 *  @param maskNum  标记作用
 *  @param otherObj 保留方法参数
 */
-(void)alterBoxTitle:(NSString*)title message:(NSString*)mesage mask:(NSInteger)maskNum idObj:(id)otherObj{
    
    __block xxx  *SZQVC = self;
    alterBox = [[ZLAlertDialog alloc] initWithTitle:title message:mesage];
    
    if (0 == maskNum) {
        [alterBox addButton:Button_CANCEL withTitle:@"OK" handler:^(ZLAlertDialogItem *item) {
            xxxx逻辑
        }];
    }
    
    if (1 == maskNum) {
        [alterBox addButton:Button_OK withTitle:@"关闭" handler:^(ZLAlertDialogItem *item) {
            //
           xxxx逻辑
            
        }];
        [alterBox addButton:Button_CANCEL withTitle:@"返回" handler:^(ZLAlertDialogItem *item) {
            xxxx逻辑
        }];
        /*
        [alterBox addButton:Button_OTHER withTitle:@"复制" handler:^(ZLAlertDialogItem *item) {
            //..复制条码到粘贴板
        }];
         */
    }

    [alterBox show];
}
View Code  弹框方法 

 

posted @ 2016-07-22 14:29  ywda  阅读(305)  评论(0编辑  收藏  举报