【iOS】—— 系统手势操作
admin
2024-01-29 23:41:11

iOS六种手势操作

在我们平时使用app的时候,会涉及到一些比较奇特的功能,比如在查看图片的时候,大部分软件我们都可以双击放大图片,包括一些按钮,它的点按和长按也会有不同的效果,对于这些要怎么实现呢?

iOS 系统在 3.2 以后,他提供了六种常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

手势操作类型

🎇

  • UIPanGestureRecognizer(拖动)
  • UIPinchGestureRecognizer(捏合)
  • UIRotationGestureRecognizer(旋转)
  • UITapGestureRecognizer(点按)
  • UILongPressGestureRecognizer(长按)
    ​- UISwipeGestureRecognizer(轻扫)

除此之外,我们还可以自定义手势,通过一个继承于UIGestureRecognizer 的子类。

手势操作状态

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible,   // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态UIGestureRecognizerStateBegan,      // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成UIGestureRecognizerStateChanged,    // 手势状态发生转变UIGestureRecognizerStateEnded,      // 手势识别操作完成(此时已经松开手指)UIGestureRecognizerStateCancelled,  // 手势被取消,恢复到默认状态UIGestureRecognizerStateFailed,     // 手势识别失败,恢复到默认状态UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded};

1.轻点手势(UITapGestureRecognizer)

    //创建手势对象(轻点)UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];//设置相关属性//点击次数(默认1)tap.numberOfTapsRequired = 1;//手指的个数(默认1)tap.numberOfTouchesRequired = 1;//添加到视图[self.testView addGestureRecognizer:tap];
//点按
- (void)tapClick:(UITapGestureRecognizer *)tap {NSLog(@"轻点手势响应!");self.view.backgroundColor = [UIColor systemPinkColor];
}

2.长按手势(UILongPressGestureRecognizer)

    //创建手势对象(长按)UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];//设置相关属性//用几个手指触屏,默认1longPressGesture.numberOfTouchesRequired = 1;//设置最短长按时间,单位为秒(默认0.5)longPressGesture.minimumPressDuration = 1;//设置手势识别期间所允许的手势可移动范围longPressGesture.allowableMovement = 10;//添加到视图[self.testView addGestureRecognizer:longPressGesture];
//长按
- (void)longPressClick:(UILongPressGestureRecognizer *)press {//state属性是所有手势父类提供的方法,用于记录手势的状态if (press.state == UIGestureRecognizerStateBegan) {NSLog(@"长按手势开始响应!");} else if (press.state == UIGestureRecognizerStateChanged) {NSLog(@"长按手势状态发生改变!");} else {NSLog(@"长按手势结束!");}
}

3.轻扫手势(UISwipeGestureRecognizer)

    //创建手势对象(左扫)UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];//设置轻扫的方向leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;//添加到视图[self.testView addGestureRecognizer:leftSwipe];//右扫UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;[self.testView addGestureRecognizer:rightSwipe];
//轻扫
-(void)swipeGestureClick:(UISwipeGestureRecognizer *)swpie{//如果是左扫if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {self.view.backgroundColor = [UIColor blueColor];NSLog(@"左扫!");} else {self.view.backgroundColor = [UIColor grayColor];NSLog(@"右扫!");}
}

4.平移手势(UIPanGestureRecognizer)

    //创建手势对象(平移)UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureClick:)];//添加到视图[self.testView addGestureRecognizer:panGesture];
//平移
- (void)panGestureClick:(UIPanGestureRecognizer *)pan {NSLog(@"响应!!");//通过pan手势,能够获取到pan.view在self.view上的偏移量CGPoint point = [pan translationInView:self.view];NSLog(@"x=%.2lf y=%.2lf",point.x,point.y);//改变中心点坐标(原来的中心点+偏移量=当前的中心点)CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);//CGPointZero<==>CGPointMake(0,0)//限制拖动范围newCenter.y = MAX(pan.view.frame.size.height/2, newCenter.y);newCenter.y = MIN(self.view.frame.size.height - pan.view.frame.size.height/2,  newCenter.y);newCenter.x = MAX(pan.view.frame.size.width/2, newCenter.x);newCenter.x = MIN(self.view.frame.size.width - pan.view.frame.size.width/2, newCenter.x);pan.view.center = newCenter;//每次调用之后,需要重置手势的偏移量,否则偏移量会自动累加[pan setTranslation:CGPointZero inView:self.view];
}

5.捏合手势(UIPinchGestureRecognizer)

    //创建手势对象(捏合)UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];//添加到视图[self.testView addGestureRecognizer:pinchGesture];
//捏合
- (void)pichGestureClick:(UIPinchGestureRecognizer *)pinch {//缩放的系数NSLog(@"%.2lf", pinch.scale);//固定写法pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//重置缩放系数(否则系数会累加)pinch.scale = 1.0;
}

6.旋转手势(UIRotationGestureRecognizer)

    //创建手势对象(旋转)UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];//添加到视图[self.testView addGestureRecognizer:rotationGesture];
//旋转
- (void)rotationGestureClick:(UIRotationGestureRecognizer *)rotation {//rotation.rotation 手势旋转的角度rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);//重置角度rotation.rotation = 20;
}

在之前看到安卓组同学写项目的时候用到了,一个左侧抽屉视图,在iOS中我尝试去找怎么写抽屉视图,查到的资料有用到第三方库的方法,但是这个方法讲的有些抽象,并没有太理解,经过学长的指导,这个抽屉视图用到的方法就是手势操作和动画之间的操作,通过平移操作来完成这一视图。

相关内容

热门资讯

摸鱼、钓虾、吃瓜、赏荷…初夏时... 这个周末,一场场充满野趣的“田园嘉年华”在沪郊金山多个农场上演,吸引众多市民带着孩子下乡来,赛跑、吃...
原创 戚... 5月28日,北京环球影城迎来了一对温暖的家庭画面:戚薇和李承铉携三岁半的儿子Seven现身游玩。现场...
滹沱河畔 遇见“诗和远方” 图为市民在滹沱河畔休闲娱乐。 初夏五月,惠风和畅。徜徉在石家庄滹沱河生态区(城区段),澄澈河水蜿蜒...
在迪士尼排队两小时,我才看清V... 文丨沈理 在网上看到一则新闻: 上海迪士尼,创极速光轮排队区。一个父亲牵着七八岁的儿子,已经在烈日...
重庆文旅喊你去吃火锅、观山水、... 本网讯(草原云·正北方网记者 马丽侠)火锅、机车、文创、演艺……5月28日下午,重庆市文化和旅游发展...