防止手指触摸移动时视图对象跳跃

发布于 2024-12-15 11:29:31 字数 572 浏览 2 评论 0原文

我在自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2024-12-22 11:29:31

在.h 文件中声明CGPoint prevPos;

这里我的视图是_rectView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    prevPos = currentPoint;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];

    if([touch view] == _rectView)
    {
        float delX =  currentPoint.x - prevPos.x;
        float delY = currentPoint.y - prevPos.y;
        CGPoint np = CGPointMake(_rectView.frame.origin.x+delX, _rectView.frame.origin.y+delY);
        //_rect.center = np;
        CGRect fr = _rectView.frame;
        fr.origin = np;
        _rectView.frame = fr;
    }

    //[self.view setNeedsDisplay];

    prevPos = currentPoint;
}

使用上面的代码。您将不会获得那种“跳跃”效果。

Declare CGPoint prevPos; in .h file.

Here my view is _rectView.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    prevPos = currentPoint;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];

    if([touch view] == _rectView)
    {
        float delX =  currentPoint.x - prevPos.x;
        float delY = currentPoint.y - prevPos.y;
        CGPoint np = CGPointMake(_rectView.frame.origin.x+delX, _rectView.frame.origin.y+delY);
        //_rect.center = np;
        CGRect fr = _rectView.frame;
        fr.origin = np;
        _rectView.frame = fr;
    }

    //[self.view setNeedsDisplay];

    prevPos = currentPoint;
}

Use the above code. You will not get that 'jump' effect.

踏雪无痕 2024-12-22 11:29:31

一种明显的方法是将触摸相对于形状中心的偏移量存储在 -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:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文