IOS中的手勢詳解

jopen 9年前發布 | 57K 次閱讀 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、拖動

 IOS中的手勢詳解

第一步:添加視圖

_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、旋轉

 IOS中的手勢詳解

第一步:添加視圖

_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、捏合

 IOS中的手勢詳解

第一步:添加視圖

_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;
}

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!