小编Osk*_*kar的帖子

设置UIView图层的锚点

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

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

谢谢.

- (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 …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uiview

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

iphone ×1

objective-c ×1

uiview ×1