设置 UIView 层的锚点 [英] Setting anchor point for UIView layer

查看:65
本文介绍了设置 UIView 层的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView 子类,我希望能够在它的超级视图中移动.当用户在 self.center 之外但在 self.bounds 内的某处触摸 UIView 时,它跳跃",因为我将新位置添加到 self.center> 实现实际移动.为了避免这种行为,我尝试设置一个锚点,让用户可以抓取并拖动视图到其边界内的任何位置.

I have a UIView subclass that I want to be able to move around within it's superview. When the user touches the UIView somewhere outside self.center but within self.bounds it "jumps" because I add the new location to self.center to achieve the actual move. To avoid this behavior I'm trying to set an anchor point that lets the user grab and drag the view anywhere within it's bounds.

我的问题是,当我计算新的锚点(如下面的代码所示)时,没有任何反应,视图根本不会改变位置.另一方面,如果我将锚点设置为预先计算的点,我可以移动视图(但当然它会跳"到预先计算的点).为什么这不能按预期工作?

My problem is that when I calculate the new anchor point (as shown in the code below) nothing happens, the view doesn't change position at all. On the other hand, if I set the anchor point to a precalculated point I can move the view (but then of course it "jumps" to the precalculated point). How come this doesn't work as expected?

谢谢.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // Only support single touches, anyObject retrieves only one touch
    UITouch *touch = [touches anyObject];
    CGPoint locationInView = [touch locationInView:self];

    // New location is somewhere within the superview
    CGPoint locationInSuperview = [touch locationInView:self.superview];

    // Set an anchorpoint that acts as starting point for the move
    // Doesn't work!
    self.layer.anchorPoint = CGPointMake(locationInView.x / self.bounds.size.width, locationInView.y / self.bounds.size.height);
    // Does work!
    self.layer.anchorPoint = CGPointMake(0.01, 0.0181818);

    // Move to new location
    self.center = locationInSuperview;
}

推荐答案

正如 Kris Van Bael 所指出的,您将需要在 touchsBegan:withEvent: 方法中按顺序进行锚点计算不要否定运动.此外,由于更改图层的 anchorPoint 将移动视图的初始位置,因此您必须向视图的 center 点添加偏移量,以避免在第一次触摸后发生跳跃".

As Kris Van Bael pointed out, your going to need to do the anchor point calculations in the touchsBegan:withEvent: method in order not to negate the movement. Additionally, since changing the layer's anchorPoint will move the view's initial position, you have to add an offset to the view's center point to avoid a 'jump' after the first touch.

您可以通过计算(并添加到您的视图的 center 点)基于初始和最终锚点(乘以您的视图的宽度/高度)之间的差异的偏移量来做到这一点,或者您可以设置视图的 center 到初始接触点.

You can do this by calculating (and adding to your view's center point) the offset based on the difference between the initial and final anchorPoints (multiplied by your view's width/height) or you could set the view's center to the initial touch point.

大概是这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint locationInView = [touch locationInView:self];
    CGPoint locationInSuperview = [touch locationInView:self.superview];

    self.layer.anchorPoint = CGPointMake(locationInView.x / self.frame.size.width, locationInView.y / self.frame.size.height);
    self.center = locationInSuperview;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint locationInSuperview = [touch locationInView:self.superview];

    self.center = locationInSuperview;
}

有关 anchorPoint 的更多信息来自 Apple 的文档 here 以及我引用的类似问题 此处.

More info on anchorPoint's from apple's docs here and a similar SO question I referenced here.

这篇关于设置 UIView 层的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆