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

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];

}}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市苞慢,隨后出現(xiàn)的幾起案子蟹演,更是在濱河造成了極大的恐慌风钻,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件酒请,死亡現(xiàn)場離奇詭異骡技,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)羞反,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門布朦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昼窗,你說我怎么就攤上這事是趴。” “怎么了澄惊?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵右遭,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么倦沧? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任线得,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好让腹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扣溺,像睡著了一般骇窍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锥余,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天腹纳,我揣著相機(jī)與錄音,去河邊找鬼驱犹。 笑死嘲恍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的雄驹。 我是一名探鬼主播佃牛,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼医舆!你這毒婦竟也來了俘侠?” 一聲冷哼從身側(cè)響起象缀,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎爷速,沒想到半個(gè)月后央星,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡遍希,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年等曼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了里烦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凿蒜。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖胁黑,靈堂內(nèi)的尸體忽然破棺而出废封,到底是詐尸還是另有隱情,我是刑警寧澤丧蘸,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布漂洋,位于F島的核電站,受9級特大地震影響力喷,放射性物質(zhì)發(fā)生泄漏刽漂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一弟孟、第九天 我趴在偏房一處隱蔽的房頂上張望贝咙。 院中可真熱鬧,春花似錦拂募、人聲如沸庭猩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蔼水。三九已至,卻和暖如春录肯,著一層夾襖步出監(jiān)牢的瞬間趴腋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工论咏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留于样,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓潘靖,卻偏偏與公主長得像穿剖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子卦溢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容