locationInView和translationInView的区别

本文深入探讨了iOS开发中locationInView和translationInView方法的区别,通过实例展示了两者在移动视图操作中的具体应用。文章通过定义SubView类并实现手势识别功能,演示了如何使用这两者来实现视图的精确移动,提供了直观的移动效果对比分析。

locationInView和translationInView的区别:

1. locationInView类UIGestureRecognizer的方法,translationInView是UIPanGestureRecognizer的方法。

2. locationInView是指当前点击在指定视图中的位置,translationInView是在指定的坐标系中移动,听起来更不知所云,举个例子会直观些(在一个view中通过手势移动另外一个view):

定义类SubView:

//.h 

@interface SubView: UIView 

@end

 @implementation abc {
   CGPoint  _initPoint.;
 }

- (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 

然后在需要使用的控制器中的viewDidLoad加上:
SubView *subView =  [[SubView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView .backgroundColor = [UIColor greenColor]; 
[self.view addSubview subView];

效果图如下:


将绿色窗口滑动到屏幕的中心,好了现在在上面红色代码行打上断点,再次往左上角移动绿色窗口, 看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左上角的作为参照坐标的(每次移动都是以上一次为原点移动)

获取屏幕触摸的XY值通常是在编写触摸事件处理程序时的操作。在不同的操作系统中,具体的实现方法有所不同: ### 在Android中: - 在Java中,你可以重写`View`或`Activity`的`onTouchEvent(MotionEvent event)`方法,通过`event.getX()``event.getY()`获取触碰点的坐标。 ```java @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); // 对触控事件进行处理... return true; // 返回true继续监听事件 } ``` - 在Kotlin中,同样获取`event.x``event.y`。 ```kotlin override fun onTouchEvent(event: MotionEvent): Boolean { val x = event.x.toInt() val y = event.y.toInt() // ... return super.onTouchEvent(event) } ``` ### 在iOS中: - Swift 中,你可以重写`UIViewController`或`UIView`的`touchesBegan(_:with:)`方法,使用`touch.location(in: view)`来获取位置。 ```swift override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { guard let touch = touches.first else { return } let location = touch.location(in: self.view) let x = location.x let y = location.y // ... } ``` - Objective-C 中,也是在`touchesBegan:withEvent:`方法中处理: ```objc - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self.view]; CGFloat x = location.x; CGFloat y = location.y; // ... } ``` 注意,这些值通常是以当前视图的坐标系为准的。如果你需要获取设备屏幕绝对坐标,可能还需要转换一下。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值