IOS中的手勢詳解,IOS手勢詳解

來源:互聯網
上載者:User

IOS中的手勢詳解,IOS手勢詳解
1、點擊

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];        //設定當前需要點擊的次數    [tap setNumberOfTapsRequired:1];    //設定當前需要觸發事件的手指數量[tap setNumberOfTouchesRequired:2];//設定當前代理tap.delegate=self;[_view addGestureRecognizer:tap];//觸發方法- (void) click{    NSLog(@"當前視圖被點擊了! ");}
2、長按
UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];//設定當前長按最小的時間長度longPress.minimumPressDuration=2;//設定允許的移動範圍 [longPress setAllowableMovement:2];[_view addGestureRecognizer:longPress];//觸發方法- (void) longPress{    NSLog(@"長按事件觸發! ");}
3、輕掃
UISwipeGestureRecognizer * swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMethod)];    //往左邊方向  swip.direction=UISwipeGestureRecognizerDirectionLeft  ;    //往右邊方向  swip.direction=UISwipeGestureRecognizerDirectionRight  ;    //往上面方向    swip.direction=UISwipeGestureRecognizerDirectionUp  ;    //往下面方向   swip.direction=UISwipeGestureRecognizerDirectionDown  ;    [_view addGestureRecognizer:swip];        //觸發方法    - (void) swipMethod{        NSLog(@"輕掃事件觸發! ");}

  如果涉及到2個以上方向的手勢最好添加多個UISwipeGestureRecognizer 對象並設定不同的方向,不要通過下面方式用符號|來串連:

swip.direction=UISwipeGestureRecognizerDirectionLeft  | UISwipeGestureRecognizerDirectionRight  
4、拖動

 

  第一步:添加視圖

_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];

  第二步:添加手勢

UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];[_view addGestureRecognizer:pan];

  第三步:實現方法

- (void) paned:(UIPanGestureRecognizer *) pan{        //擷取移動的大小    CGPoint point= [pan translationInView:pan.view];    //更改視圖的中心點座標    CGPoint points=_view.center;    points.x+=point.x;    points.y+=point.y;    _view.center=points;    //每次都清空一下消除座標疊加    [pan setTranslation:CGPointZero inView:pan.view];}
5、旋轉

 

  第一步:添加視圖

_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];

  第二步:添加手勢

UIRotationGestureRecognizer * roate=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];    [_view addGestureRecognizer:roate];roate.delegate=self;

  第三步:實現方法

- (void) rotate:(UIRotationGestureRecognizer *) rote{   //擷取當前旋轉的度數   CGFloat rotation= rote.rotation;    //通過仿射變換實現旋轉  _view.transform=CGAffineTransformRotate(_view.transform, rotation);    //防止旋轉疊加需要清零    rote.rotation=0;}
6、捏合

 

  第一步:添加視圖

_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];

  第二步:添加手勢

UIPinchGestureRecognizer * pich=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(piches:)];[_view addGestureRecognizer:pich];pich.delegate=self;

  第三步:實現方法

- (void) piches:(UIPinchGestureRecognizer *) pich{    //擷取比例    CGFloat scale=pich.scale;    //通過仿射變換實現縮放    _view.transform=CGAffineTransformScale(_view.transform, scale, scale);    //防止比例疊加需要置為1    pich.scale=1; }

 

【補充】如果需要同時響應多個手勢需要重寫代理方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    return YES;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.