UITouch
1.UITouch *touch = [touches anyObject];//找到触摸事件
if([touch.view isKindOfClass:[UIImageView class]]){
CGPoint point = [touch locationInView:self.view];//找到点击事件在self.view里的坐标
CGFloat xMove = point.x - _recordPoint.x;
CGFloat yMove = point.y - _recordPoint.y;//计算运动轨迹
touch.view.center = CGPointMake(touch.view.center.x +xMove,touch.view.center.y + yMove);//根据运动轨迹移动到点击到的视图
_recordPoint = [touch locationInView:self.view];//每次移动以后都修改记录
2.根据点击的次数来监听
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"====%ld",touch.tapCount); //连续点击的次数 if ([touch.view isKindOfClass:[UIImageView class]]) { if (touch.tapCount == 2) { CGRect rect = touch.view.frame; rect.origin.x -= 5; rect.size.width += 10; touch.view.frame = rect; //修改frame } }}
各种手势
1.点击手势(类似于btn的点击)
UITapGestureRecognizer *tapGR = [[UITapgestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
[view addGestureRecognizer:tapGR];
- (void)tapGR:(UITapGestureRecognizer *)tapGR
{
[self.view bringSubViewToFront:tapGR.view];//将手势对应的view移到最上层
}
2.移动手势
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
[view addGestureRecognizer:panGR];
1 - (void)panGR:(UIPanGestureRecognizer *)panGR 2 { 3 CGPoint translation = [panGR translationInView:self.view]; 4 //获取手势移动的轨迹 5 6 panGR.view.center = CGPointMake(panGR.view.center.x + translation.x, panGR.view.center.y + translation.y); 7 //根据手势的轨迹移动view 8 9 [panGR setTranslation:CGPointZero inView:self.view];10 //每次移动以后都清空手势记录的轨迹11 }
3.捏合手势
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGR:)]; [view addGestureRecognizer:pinchGR];
1 - (void)pinchGR:(UIPinchGestureRecognizer *)pinchGR2 {3 pinchGR.view.transform = CGAffineTransformScale(pinchGR.view.transform, pinchGR.scale, pinchGR.scale);4 //让view随着手势的缩放而缩放5 6 pinchGR.scale = 1;7 //缩放以后需要还原手势中记录的倍数8 }
4.旋转手势
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGR:)]; [view addGestureRecognizer:rotationGR];
1 - (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR2 {3 rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);4 //让view随着手势的旋转而旋转5 6 rotationGR.rotation = 0;7 //旋转以后还原记录8 }
5.长按手势
UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGR:)]; [view addGestureRecognizer:longGR];
1 - (void)longGR:(UILongPressGestureRecognizer *)longGR 2 { 3 NSLog(@"===%ld",longGR.state); 4 //打印手势的状态(开始响应,改变,结束响应) 5 6 if (longGR.state == UIGestureRecognizerStateBegan) { 7 //长按手势开始响应时触发 8 9 CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];10 //创建一个基础动画11 12 basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];13 basicAnimation.toValue = @2;14 //从哪里来,到哪里去15 16 basicAnimation.autoreverses = YES;17 //是否以动画方式返回18 19 basicAnimation.duration = 0.1;20 //单次动画时间21 22 basicAnimation.repeatCount = 99;23 //重复次数24 25 [longGR.view.layer addAnimation:basicAnimation forKey:@"aa"];26 //添加动画27 }28 }
6.轻扫手势(和移动手势有冲突)
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGR:)]; //轻扫手势(和pan移动有冲突的) swipeGR.direction = UISwipeGestureRecognizerDirectionUp; //设置轻扫的方向 [view addGestureRecognizer:swipeGR];
如果想实现多个方向的轻扫手势,需要创建多个,然后设置不同的方向。。。。。
1 - (void)swipeGR:(UISwipeGestureRecognizer *)swipeGR 2 { 3 CGRect rect = swipeGR.view.frame; 4 5 //判断方向 6 if (swipeGR.direction == UISwipeGestureRecognizerDirectionUp) { 7 rect.origin.y -= 5; 8 rect.size.height += 5; 9 }10 11 //& 按位与判断(这种用法只能是枚举值后面有1<<2这种位操作的)12 if (swipeGR.direction & UISwipeGestureRecognizerDirectionLeft) {13 rect.origin.x -= 5;14 rect.size.width += 5;15 }16 17 swipeGR.view.frame = rect;18 }