iOS 手勢滑動事件綁定

jopen 9年前發布 | 14K 次閱讀 IOS iOS開發 移動開發

UIGestureRecognizer 手勢響應基類
- UITapGestureRecognizer //點擊手勢識別器,可以是點擊一次,或多次都能識別
- UIPinchGestureRecognizer //捏合手勢識別器,用于視圖的放大縮小
- UIRotationGestureRecognizer //旋轉手勢識別器
- UISwipeGestureRecognizer //滑動手勢識別器,向上、下、左、右滑動
- UIPanGestureRecognizer //拖動手勢識別器
- UILongPressGestureRecognizer //長按手勢識別器,常見的有長按跳出一個界面用以編輯


這里演示的是

UISwipeGestureRecognizer  // 上下左右的滑動

ps: 參數詳解:self設置代理類,@selector設置事件響應,

然后要設置響應的是哪個view的手勢

這里設置的是

self.gameView

/*
 * 綁定手勢事件,上下左右
 **/
- (void)bindAction {
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.gameView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.gameView addGestureRecognizer:recognizer];
   
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeTop)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.gameView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeBottom)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.gameView addGestureRecognizer:recognizer];
}



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