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

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

问题描述

我有一个UIView子类,我希望能够在它的superview中移动。当用户在 self.center 之外的某处触摸UIView但在 self.bounds 内它会跳,因为我添加了新位置到 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 将移动视图的初始位置,您必须向视图的中心添加偏移量指向第一次触摸后避免跳跃。

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.

您可以通过计算(并添加到视图的中心点)基于两者之间的差异来实现此目的初始和最终的anchorPoints(乘以你的视图的宽度/高度)或者你可以将视图的 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 来自苹果公司的文档这里和类似的SO问题我参考了这里

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

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

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