locationInView和translationInView的区别:
1. locationInView类UIGestureRecognizer的方法,translationInView是UIPanGestureRecognizer的方法。
2. locationInView是指当前点击在指定视图中的位置,translationInView是在指定的坐标系中移动,听起来更不知所云,举个例子会直观些(在一个view中通过手势移动另外一个view):
定义类SubView:
//.h
@interface SubView: UIView
- (void)drawRect:(CGRect)rect {
UIPanGestureRecognizer *panGuest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:panGuest];
}
- (void)pan:(UIPanGestureRecognizer*)g {
CGPoint p = [g translationInView:self.superview];
CGPoint pp = [g locationInView:self.superview];
if (g.state == UIGestureRecognizerStateBegan) {
_initPoint = self.center;
}
self.center = CGPointMake(_initPoint.x + p.x, _initPoint.y + p.y);
}
@end
效果图如下:
将绿色窗口滑动到屏幕的中心,好了现在在上面红色代码行打上断点,再次往左上角移动绿色窗口, 看p和pp的值,此时
1.p(0,-1.0)pp(159, 209)
2.p (-22.5, -39.5) pp(136,5, 170)
断点跑完了之后,再次往左上角移动绿色窗口:
1.p(0,-4.5)pp(127, 165)
2.p (-13.5, -36.5) pp(114.5, 134.5)
发现规律了吗!我们知道 p 对应的是 translationInView:self.superview;
pp =对应的是 locationInView:self.superview;
看p坐标你会发现p是每次引动都是以被移动(绿色view)的左上角的作为参照坐标的(每次移动都是从原点(0,0)为其实坐标)
看pp引移动的view的父view左上角的作为参照坐标的(每次移动都是以上一次为原点移动)