UIGestureRecognizer類(lèi),用來(lái)檢測(cè),識(shí)別用戶(hù)使用設(shè)備時(shí)所用的手勢(shì),定義了所有手勢(shì)的基本行為.以下是UIGestureRecognizer子類(lèi),喲關(guān)于處理具體的用戶(hù)手勢(shì)行為。
單擊手勢(shì)
單擊手勢(shì)UITapGestureRecognizerUITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)]; [self.view addGestureRecognizer:singleTap];//單擊事件-(void)singleTap:(UITapGestureRecognizer *)tapgestrue{ NSLog(@"單擊");}
雙擊手勢(shì)
UITapGestureRecognizerUITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)]; doubleTap.numberOfTapsRequired=2; [self.view addGestureRecognizer:doubleTap]; //區(qū)別單擊雙擊手勢(shì) [singleTap requireGestureRecognizerToFail:doubleTap];//雙擊點(diǎn)擊事件-(void)doubleTap:(UITapGestureRecognizer *)taggestrue{ NSLog(@"雙擊");}
輕掃手勢(shì)
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; //設(shè)置輕掃方向,默認(rèn)向右 swipeGesture.direction=UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipeGesture];//輕掃事件-(void)swipe:(UISwipeGestureRecognizer *)swipeGesture{ NSLog(@"輕掃手勢(shì)");}
//平移
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; [self.view addGestureRecognizer:panGesture]; //平移事件-(void)pan:(UIPanGestureRecognizer *)pan{ CGPoint point=[pan locationInView:self.view]; NSLog(@"%@",NSStringFromCGPoint(point));}
//長(zhǎng)按手勢(shì)
UILongPRessGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; longPressGesture.minimumPressDuration=2; [self.view addGestureRecognizer:longPressGesture];//長(zhǎng)按手勢(shì)事件-(void)longPress:(UILongPressGestureRecognizer *)longPress{ //長(zhǎng)按離開(kāi)時(shí)一會(huì)調(diào)用一次,所以需要設(shè)置手勢(shì)狀態(tài) if(longPress.state==UIGestureRecognizerStateEnded) { return; } NSLog(@"長(zhǎng)按超過(guò)兩秒");}
//旋轉(zhuǎn)手勢(shì)
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; [self.view addGestureRecognizer:rotation];//旋轉(zhuǎn)事件-(void)rotation:(UIRotationGestureRecognizer *)rotation{ //根據(jù)旋轉(zhuǎn)的弧度獲得角度 float degree=rotation.rotation*(180/M_PI); NSLog(@"%f",degree);}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注