【问题标题】:Move UIView in specific area在特定区域移动 UIView
【发布时间】:2014-11-08 01:53:09
【问题描述】:

我使用下面的代码来移动一个 UIImageView

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    paddle.center = CGPointMake(location.x, paddle.center.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesBegan:touches withEvent:event];
}

效果很好,从左到右移动。但我不知道如何避免在屏幕上移动,我打算移动它但不要与屏幕的左右边缘相交。假设左边 10 个像素,右边 10 个像素。

【问题讨论】:

    标签: ios objective-c touch-event


    【解决方案1】:

    首先,你是这样说的:

    paddle.center = CGPointMake(location.x, paddle.center.y);
    

    所以paddle 在哪里,完全取决于你。如果您不希望它处于某个 x 值,请不要将其设置为该 x 值。

    其次,永远不要从touchesMoved 内部调用touchesBegan

    第三,使用 UIPanGestureRecognizer 会让您更快乐、更安全。这样的情况正是它的用途。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多