通过CGAffineTransformMakeScale缩放自定义UIView会中断使用touchesMoved进行拖动

时间:2014-03-06 12:08:15

标签: ios iphone uiview cgaffinetransform cgaffinetransformscale

我已准备好并检查了GitHub a simple test case,以便拖动自定义UIView

app screenshot

效果很好,拖拽在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));
}

然而,瓷砖对于my actual game来说太大了(一旦UIScrollView中的游戏板被放大,我将不得不调整它们的缩放比例):

app screenshot

所以我通过调用

来缩放我的自定义UIView
tile.transform = CGAffineTransformMakeScale(.5, .5);

尺寸会发生变化,但瓷砖会根据需要“快速”拖动两倍:

app screenshot

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

1 个答案:

答案 0 :(得分: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));
}