我有一个UIView子类,我想让它能够在其父视图中移动。当用户触摸UIView的某个位置,该位置位于self.bounds内但不在self.center内时,它会“跳跃”,因为我将新位置添加到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 to new location
self.center = locationInSuperview;
}