iOS 模仿微信長按識別二維碼

G20過去了,也剛忙一陣子.今天來一個模仿微信長按識別二維碼,,,比較簡單.望大家勿噴.還是老規則上代碼

#import"ViewController.h"

@interfaceViewController()

{

NSTimer*_timer;

}

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

UIImageView*zarimage = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,200,200)];

zarimage.image= [UIImageimageNamed:@"Football-ball@3x"];

zarimage.userInteractionEnabled=YES;

UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(dealLongPress:)];

[zarimageaddGestureRecognizer:longPress];

[self.viewaddSubview:zarimage];// Do any additional setup after loading the view, typically from a nib.}

#pragma mark->長按識別二維碼

-(void)dealLongPress:(UIGestureRecognizer*)gesture{

if(gesture.state==UIGestureRecognizerStateBegan){

_timer.fireDate=[NSDatedistantFuture];

UIImageView*tempImageView=(UIImageView*)gesture.view;

if(tempImageView.image){

//1.初始化掃描儀,設置設別類型和識別質量

CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

//2.掃描獲取的特征組

NSArray*features = [detectorfeaturesInImage:[CIImageimageWithCGImage:tempImageView.image.CGImage]];

if(features.count>0) {

CIQRCodeFeature*feature = [featuresobjectAtIndex:0];

NSString*scannedResult = feature.messageString;

UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction*ok = [UIAlertActionactionWithTitle:@"發送給朋友"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*ok1 = [UIAlertActionactionWithTitle:@"收藏"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*ok2 = [UIAlertActionactionWithTitle:@"保存圖片"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*ok3 = [UIAlertActionactionWithTitle:@"識別圖中二維碼"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"確定");

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:scannedResult]];

}];

UIAlertAction*cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) {

NSLog(@"取消");

}];

if(![cancelvalueForKey:@"_titleTextColor"]) {

[cancelsetValue:[UIColorredColor]forKey:@"_titleTextColor"];

}

[alertaddAction:ok];//添加按鈕

[alertaddAction:ok1];//添加按鈕

[alertaddAction:ok2];//添加按鈕

[alertaddAction:ok3];//添加按鈕

[alertaddAction:cancel];//添加按鈕

//以modal的形式

[selfpresentViewController:alertanimated:YEScompletion:^{ }];

}else{

UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction*ok = [UIAlertActionactionWithTitle:@"發送給朋友"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*ok1 = [UIAlertAction actionWithTitle:@"收藏"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*ok2 = [UIAlertAction actionWithTitle:@"保存圖片"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {

NSLog(@"確定");

}];

UIAlertAction*cancel = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {

NSLog(@"取消");

}];

if([cancel valueForKey:@"titleTextColor"]) {

[cancel setValue:[UIColor redColor]forKey:@"titleTextColor"];

}

[alertaddAction:ok];//添加按鈕

[alertaddAction:ok1];//添加按鈕

[alert addAction:ok2];//添加按鈕

[alert addAction:cancel];//添加按鈕

NSLog(@"22222222");

//以modal的形式

[selfpresentViewController:alertanimated:YES completion:^{ }];

}//3.獲取掃描結果

}else{

UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"掃描結果"message:@"您還沒有生成二維碼"delegate:nil cancelButtonTitle:@"確定"otherButtonTitles:nil,nil];

[alertViewshow];

}

}else if(gesture.state==UIGestureRecognizerStateEnded){

_timer.fireDate=[NSDate distantPast];

}}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容