iOS手势
iOS开发中遇到的手势相关的动作有以下几类(Apple iOS框架提供的简单实现)UITapGestureRecognizer(包含单击、双击、多次点击等等)
UILongPressGestureRecognizer(长按手势)
UIPanGestureRecognizer(平移手势)
UISwipeGestureRecognizer(手指在屏幕上很快的滑动)
UIPinchGestureRecognizer(手指缩放操作)
UIRotationGestureRecognizer(手指旋转操作)
向指定View添加手势
UIPanGestureRecognizer *panRecognizer = [initWithTarget:self action:@selector(handlePanFrom:)];;panRecognizer.maximumNumberOfTouches = 1;panRecognizer.delegate = self;
从指定view移除手势
;
1.点击手势(单击、双击)
UITapGestureRecognizer *singleTap = [ initWithTarget:self action:@selector(handleSingleTap:)];;singleTap.delegate = self;;//1次点击;//1个手指操作UITapGestureRecognizer *doubleTap = [ initWithTarget:self action:@selector(handleDoubleTap:)];;;;//2次点击;//2个手指操作;//防止双击触发两次单击事件;
在hadnleSingleTap:和handleDoubleTap:中进行单击和双击事件的处理
2.长按手势
UILongPressGestureRecognizer *longPressGesture = [ initWithTarget:self action:@selector(handleLongPress:)];;;//最短按下持续时间1秒
在handleLongPress:方法中进行长按时间的处理
3.平移手势
UIPanGestureRecognizer *panRecognizer = [initWithTarget:self action:@selector(handlePanFrom:)];;;;
在handlePanFrom:方法中进行平移手势的处理
-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer{ //拿到手指目前的位置 CGPoint location = ; CGPoint tranlation = ; switch (recognizer.state) { //手指动作开始 case UIGestureRecognizerStateBegan:// aView.transform = CGAffineTransformIdentity; break; //手指动作进行 case UIGestureRecognizerStateChanged: //监听拖动动作 //如果已经在起始位置则不允许再向右拖动 if (tranlation.x <= 0) { aView.transform = CGAffineTransformMakeTranslation(tranlation.x, 0); } break; //手指动作结束 case UIGestureRecognizerStateEnded: { //得到拖动速度 CGPoint v = ; //拖动速度大于500则直接remove视图 if (v.x < -500) { ; } else { if (tranlation.x <= 0) { aView.transform = CGAffineTransformMakeTranslation(tranlation.x, 0); } //如果手指当前位置大于280像素处,remove视图 if (location.x < 80) { ; } else { //否则视图回到起始位置 ; } } } break; case UIGestureRecognizerStateCancelled: case UIGestureRecognizerStateFailed: break; default: break; }}
4.快速滑动手势
UISwipeGestureRecognizer *leftSwipe = [ initWithTarget:self action:@selector(handleSwipeFrom:)];;;UISwipeGestureRecognizer *rightSwipe = [ initWithTarget:self action:@selector(handleSwipeFrom)];;;
在handleSwipe:方法中处理快速滑动事件
- (void)handleSwipe:(UISwipeGestureRecognizer *)sender{switch (sender.direction) { case UISwipeGestureRecognizerDirectionLeft: { imgView = [ initWithFrame:CGRectMake(kDeviceWidth, 0, 120, 160)]; ]; ; ; } break; case UISwipeGestureRecognizerDirectionRight: { imgView = [ initWithFrame:CGRectMake(-kDeviceWidth, 0, 120, 160)]; ]; ; ; } break; default: break; }}
5.缩放手势和旋转手势,待利用到再补充
页:
[1]