G20過去了,也剛忙一陣子.今天來一個(gè)模仿微信長按識別二維碼,,,比較簡單.望大家勿噴.還是老規(guī)則上代碼
#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.初始化掃描儀衣摩,設(shè)置設(shè)別類型和識別質(zhì)量
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:@"發(fā)送給朋友"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:@"發(fā)送給朋友"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.獲取掃描結(jié)果
}else{
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"掃描結(jié)果"message:@"您還沒有生成二維碼"delegate:nil cancelButtonTitle:@"確定"otherButtonTitles:nil,nil];
[alertViewshow];
}
}else if(gesture.state==UIGestureRecognizerStateEnded){
_timer.fireDate=[NSDate distantPast];
}}