防止手指触摸移动时视图对象跳跃
我在自定义 UIView 中创建了一个 200x200 的圆圈。我正在使用以下脚本在 iPad 的屏幕上移动视图对象。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if([touch view] == newShape)
{
newShape.center = currentPoint;
}
[self.view setNeedsDisplay];
}
一切正常,我可以将圆圈移动到屏幕上的任何位置。但是,如果我不触摸圆形物体的死点,它会稍微跳跃。通过阅读代码,这是非常明显的,因为 newShape.center
被设置为触摸发生的任何位置,并最终快速捕捉到该位置。
我正在寻找一种在不捕捉到触摸位置的情况下移动对象的方法。我想我会使用 xy 坐标来实现这一点,但我不确定如何实现它。
谢谢!
I've created a 200x200 circle in a custom UIView. I am using the following script to move the view object around the screen on the iPad.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if([touch view] == newShape)
{
newShape.center = currentPoint;
}
[self.view setNeedsDisplay];
}
Everything works fine, I can move the circle anywhere on the screen. However, if I don't touch dead center on the circle object, it jumps slightly. By reading the code, this is quite obvious because newShape.center
is being set to wherever the touch happens and ultimately snapping quickly to that position.
I'm looking for a way to move an object without snapping to the touch position. I'm thinking I would use xy coordinates to achieve this, but I'm not sure how to implement it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在.h 文件中声明
CGPoint prevPos;
。这里我的视图是
_rectView
。使用上面的代码。您将不会获得那种“跳跃”效果。
Declare
CGPoint prevPos;
in .h file.Here my view is
_rectView
.Use the above code. You will not get that 'jump' effect.
一种明显的方法是将触摸相对于形状中心的偏移量存储在 -touchesBegan:withEvent: 中,并将该偏移量应用到
-touchesMoved:withEvent:
中。An obvious way to do it is to store the offset of the touch from the shape's center in
-touchesBegan:withEvent:
and apply the offset in-touchesMoved:withEvent:
.