在我们平时使用app的时候,会涉及到一些比较奇特的功能,比如在查看图片的时候,大部分软件我们都可以双击放大图片,包括一些按钮,它的点按和长按也会有不同的效果,对于这些要怎么实现呢?
iOS 系统在 3.2 以后,他提供了六种常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。
🎇
除此之外,我们还可以自定义手势,通过一个继承于UIGestureRecognizer 的子类。
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成UIGestureRecognizerStateChanged, // 手势状态发生转变UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded};
//创建手势对象(轻点)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];
}
//创建手势对象(长按)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(@"长按手势结束!");}
}
//创建手势对象(左扫)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(@"右扫!");}
}
//创建手势对象(平移)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];
}
//创建手势对象(捏合)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;
}
//创建手势对象(旋转)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中我尝试去找怎么写抽屉视图,查到的资料有用到第三方库的方法,但是这个方法讲的有些抽象,并没有太理解,经过学长的指导,这个抽屉视图用到的方法就是手势操作和动画之间的操作,通过平移操作来完成这一视图。