iOS 常见的六种 手势

六种手势如下:

UITapGestureRecognizer

[UIPinchGestureRecognizer](捏合)

[UIRotationGestureRecognizer](旋转)

([UISwipeGestureRecognizer]滑动,快速移动)

[UIPanGestureRecognizer](平移,慢速移动)

[UILongPressGestureRecognizer](长按)

以上六种只是常用的一些手势,在这里作此简述

创建图片对象

// 创建图片对象

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

// 设置图片内容

imageView.image = [UIImage imageNamed:@“自己图片名”];

// 设置图片居中

imageView.center = self.view.center;

// 设置用户交互

imageView.userInteractionEnabled = YES;

// 添加到视图上

[self.view addSubview:imageView];
UITapGestureRecognizer (点击手势)

//1.创建手势对象

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

//2.手势相关的属性

//点击次数(默认1)

tapGesture.numberOfTapsRequired = 1;

//手指的个数(默认1)

tapGesture.numberOfTouchesRequired = 1;

//3.把手势与视图相关联

[imageView addGestureRecognizer:tapGesture];
// 点击事件

-(void)tapClick:(UITapGestureRecognizer *)tap{

NSLog(@“点击图片了”);

}
UIPinchGestureRecognizer (捏合手势)

//捏合手势

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichClick:)];

[imageView addGestureRecognizer:pinchGesture];

// 捏合事件

-(void)pichClick:(UIPinchGestureRecognizer *)pinch{

//缩放的系数

NSLog(@"%.2lf",pinch.scale);

//固定写法

pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

//重置缩放系数(否则系数会累加)

pinch.scale = 1.0;

}
UIRotationGestureRecognizer (旋转手势)

//旋转手势

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];

[imageView addGestureRecognizer:rotationGesture];

-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{

NSLog(@“qqq”);

}
UISwipeGestureRecognizer (滑动手势)

// 滑动手势

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];

// 设置轻扫的方向

leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

[self.view addGestureRecognizer:leftSwipe];

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];

// 右扫

rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:rightSwipe];

// 滑动事件

-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{

// 如果是左扫

if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {

NSLog(@“左扫!”);

}else{

NSLog(@“右扫!”);

}
}

UIPanGestureRecognizer (平移手势)

//平移手势

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];

[imageView addGestureRecognizer:panGesture];

// 平移事件

-(void)panClick:(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];

}
UILongPressGestureRecognizer (长按手势)

//长按手势

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];

//用几个手指触屏,默认1

longPressGesture.numberOfTouchesRequired = 1;

//设置最短长按时间,单位为秒(默认0.5)

longPressGesture.minimumPressDuration = 1;

//设置手势识别期间所允许的手势可移动范围

longPressGesture.allowableMovement = 10;

[imageView addGestureRecognizer:longPressGesture];

// 长按事件

-(void)longPressClick:(UILongPressGestureRecognizer *)press{

//state属性是所有手势父类提供的方法,用于记录手势的状态

if(press.state == UIGestureRecognizerStateBegan){

NSLog(@“长按手势开始响应!”);

}else if (press.state == UIGestureRecognizerStateChanged){

NSLog(@“长按手势状态发生改变!”);

}else{

NSLog(@“长按手势结束!”);

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值