本文主要是介绍iOS之touch手势用法/locationInView:与translationInView:的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//开始点击
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];//获取一个触摸对象
CGPoint cur=[touch locationInView:self.view];//当前点
CGPoint pre=[touch previousLocationInView:self.view];//上一个点
CGPoint dd=[touch preciseLocationInView:self.view];
NSLog(@"--%f--%f---%f",cur.x,pre.x,dd.x);
}
//点击结束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
//移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
//取消点击
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
一、locationInView:与translationInView:的区别
- (void)viewDidLoad {
[superviewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(move:)];
[self.viewaddGestureRecognizer:pan];
}
- (void)move:(UIPanGestureRecognizer *)pan {
CGPoint point = [pan locationInView:self.view];//self.view是手势作用在哪个view上。以父 view左上角为原点;
CGPoint transPoint = [pan translationInView:self.view];//以自身的左上角为原点;每次移动后,原点都置0;计算的是相对于上一个位置的偏移;
NSLog(@"locationInView:%f--%f\n -- translationInView:%f--%f",point.x,point.y,transPoint.x,transPoint.y);
CGRectContainsPoint(button.frame, point);----判断点是否在button上;
}
打印如下:
2016-04-22 11:06:02.212 xdSchoolChat[13082:1214518] locationInView:136.000000--308.000000
-- translationInView:3.000000--0.000000
2016-04-22 11:06:02.229 xdSchoolChat[13082:1214518] locationInView:141.000000--312.000000
-- translationInView:8.000000--4.000000
2016-04-22 11:06:02.229 xdSchoolChat[13082:1214518] locationInView:141.000000--312.000000
-- translationInView:8.000000--4.000000
2016-04-22 11:06:02.246 xdSchoolChat[13082:1214518] locationInView:147.000000--316.500000
-- translationInView:14.000000--8.500000
2016-04-22 11:06:02.263 xdSchoolChat[13082:1214518] locationInView:151.500000--320.000000
-- translationInView:18.500000--12.000000
2016-04-22 11:06:02.281 xdSchoolChat[13082:1214518] locationInView:154.500000--323.000000
-- translationInView:21.500000--15.000000
2016-04-22 11:06:02.298 xdSchoolChat[13082:1214518] locationInView:157.500000--325.500000
-- translationInView:24.500000--17.500000
locationInView:获取到的是手指点击屏幕实时的坐标点;
translationInView:获取到的是手指移动后,相对于手势第一次作用在view上的点的偏移量。
这篇关于iOS之touch手势用法/locationInView:与translationInView:的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!