1

我已经准备好并在 GitHub 中检查了一个用于拖动自定义的简单测试用例UIView

应用截图

它运行良好,并且在Tile.m中实现了拖动:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    CGPoint previous = [touch previousLocationInView:self];
    self.frame = CGRectOffset(self.frame,
                              (location.x - previous.x),
                              (location.y - previous.y));
}

但是,对于我的实际游戏来说,这些图块太大了(UIScrollView无论如何,一旦游戏板被放大,我就必须调整它们的缩放比例):

应用截图

UIView所以我通过调用来缩小我的自定义

tile.transform = CGAffineTransformMakeScale(.5, .5);

大小会发生变化,但瓷砖会根据需要突然“以两倍的速度”拖动:

应用截图

可能touchesMoved应该调整代码(上面的代码),但我不确定transform操作破坏了什么?

4

1 回答 1

0

没关系,我找到了解决方案:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    CGPoint previous = [touch previousLocationInView:self];

    if (!CGAffineTransformIsIdentity(self.transform)) {
        location = CGPointApplyAffineTransform(location, self.transform);
        previous = CGPointApplyAffineTransform(previous, self.transform);
    }

    self.frame = CGRectOffset(self.frame,
                              (location.x - previous.x),
                              (location.y - previous.y));
}
于 2014-03-06T12:25:40.023 回答