iOS中常用的六种手势分别为点击,长按,清扫,捏合,旋转,拖拽,手势类继承于UIController父类,所以有触发事件.所有控件都可以添加手势,但是UIImageView和UILable的用户交互是默认关闭的,只有把他们的userInteractionEnable属性设置为YES才可以触发事件;
1 点击:点击手势就是点击一下,然后就会执行触发事件;
创建手势的语句为
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];
[view addGestureRecognizer:tap];
[tap release];
其中self为创建手势所在的类的虚拟对象,tap:为执行点击手势后触发的事件,下面的语句是把手势给视图view;- (void)tap:(UITapGestureRecognizer *)tap
{
}
这是手势tap的触发事件;
2 长按:长按顾名思义长时间按压
创建语句为:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(press:)];
[view addGestureRecognizer:longPress];
[longPress release];
触事件为:- (void)longPress:(UILongPressGestureRecognizer *)press
{
}
创建语句:
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipAction:)];
// 滑动方向
swip.direction =UISwipeGestureRecognizerDirectionRight;
[view addGestureRecognizer:swip];
[swip release];
触发事件为:
- (void)swipAction:(UISwipeGestureRecognizer *)swip
{
// 打印是由那个方法调用的什么方法
NSLog(@"%s", __func__);
}
创建语句:
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
[view addGestureRecognizer:pinch];
[pinch release];
触发事件:- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"%s", __func__);
// 通过tag值传值
UIView *view = [self.viewviewWithTag:100];
// 设定放大倍数,让它长宽等比例放大
view.transform =CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
// 每次放大后还原放大倍数,
pinch.scale = 1;
}
5 旋转:可以把视图旋转
创建语句:
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotateAction:)];
[view addGestureRecognizer:rotate];
[rotate release];
触发事件:
- (void)rotateAction:(UIRotationGestureRecognizer *)rotate
{
NSLog(@"%s", __func__);
UIView *view = [self.viewviewWithTag:100];
// 设定旋转角度
view.transform =CGAffineTransformRotate(view.transform, rotate.rotation);
// 每次旋转玩之后旋转角度清零
rotate.rotation = 0;
}
6 拖拽:可以移动视图创建语句:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
[view addGestureRecognizer:pan];
[pan release];
触发事件:
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"%s", __func__);
UIView *view = [self.viewviewWithTag:100];
// 获取当前视图的偏移量
CGPoint point = [pan translationInView:view];
// 设置偏移量
view.transform =CGAffineTransformTranslate(view.transform, point.x, point.y);
// 偏移量清零
[pan setTranslation:CGPointMake(0, 0)inView:view];
}
以上就是常见的六种手势及其用法;