UIGestureRecognizer手勢操作,uigesturerecognizer
UIGestureRecognizer類,用來檢測,識別使用者使用裝置時所用的手勢,定義了所有手勢的基本行為.以下是UIGestureRecognizer子類,喲關於處理具體的使用者手勢行為。
單擊手勢
單擊手勢UITapGestureRecognizerUITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)]; [self.view addGestureRecognizer:singleTap];//單擊事件-(void)singleTap:(UITapGestureRecognizer *)tapgestrue{ NSLog(@"單擊");}
雙擊手勢
UITapGestureRecognizerUITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)]; doubleTap.numberOfTapsRequired=2; [self.view addGestureRecognizer:doubleTap]; //區別單擊雙擊手勢 [singleTap requireGestureRecognizerToFail:doubleTap];//雙擊點擊事件-(void)doubleTap:(UITapGestureRecognizer *)taggestrue{ NSLog(@"雙擊");}
撥動手勢
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; //設定輕掃方向,預設向右 swipeGesture.direction=UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipeGesture];//輕掃事件-(void)swipe:(UISwipeGestureRecognizer *)swipeGesture{ NSLog(@"撥動手勢");}
//平移
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));}
//長按手勢
UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; longPressGesture.minimumPressDuration=2; [self.view addGestureRecognizer:longPressGesture];//長按手勢事件-(void)longPress:(UILongPressGestureRecognizer *)longPress{ //長按離開時一會調用一次,所以需要設定手勢狀態 if(longPress.state==UIGestureRecognizerStateEnded) { return; } NSLog(@"長按超過兩秒");}
//旋轉手勢
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; [self.view addGestureRecognizer:rotation];//旋轉事件-(void)rotation:(UIRotationGestureRecognizer *)rotation{ //根據旋轉的弧度獲得角度 float degree=rotation.rotation*(180/M_PI); NSLog(@"%f",degree);}