使用阴影拖动自定义UIView - 大小重置并且阴影消失

时间:2014-03-04 20:14:12

标签: ios iphone uigesturerecognizer uipangesturerecognizer touchesbegan

在Xcode 5中,我创建了an iPhone app,其中包含5个“字母图块”,可以拖动它们:

app screenshot

使用Tile.xib(此处为Tile)将图块实现为fullscreen类:

Xcode screenshot

tile.png是一张没有阴影的小图片:

tile.png

dragged.png是带阴影的较大图片:

dragged.png

后一张图片由Tile.m中的touchesBegan显示:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    _background.image = kDragged;
    [_letter setFont:[UIFont systemFontOfSize:48]];
    [_value setFont:[UIFont systemFontOfSize:20]];

    [self.superview bringSubviewToFront:self];

    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    _background.image = kTile;
    [_letter setFont:[UIFont systemFontOfSize:36]];
    [_value setFont:[UIFont systemFontOfSize:16]];

    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
    _background.image = kTile;
    [_letter setFont:[UIFont systemFontOfSize:36]];
    [_value setFont:[UIFont systemFontOfSize:16]];

    [super touchesCancelled:touches withEvent:event];
}

使用ViewController.m中的UIPanGestureRecognizer完成拖动:

- (IBAction)dragTile:(UIPanGestureRecognizer *)recognizer
{
    Tile* tile = (Tile*)recognizer.view;
    UIView* parent = tile.superview;

    if (recognizer.state == UIGestureRecognizerStateBegan ||
        recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:parent];

        [tile setCenter:CGPointMake(tile.center.x + translation.x,
                                    tile.center.y + translation.y)];

        [recognizer setTranslation:CGPointZero inView:parent];
    }
}

我的问题是:

当我触摸一块瓷砖时,它的尺寸会增加并显示阴影(这是可以的)。

但是一旦我开始拖动瓷砖,它的尺寸会重新变回小而没有阴影(我不明白)。

我在touchesEndedtouchesCancelled设置了断点 - 当拖动开始时,后者正在被击中。但是为什么以及如何制止这个?

1 个答案:

答案 0 :(得分:1)

正如评论中所讨论的,该问题的解决方案是将cancelsTouchesInView的{​​{1}}属性设置为UIGestureRecognizer,以便NO方法不会被touchesCancelled:withEvent:方法调用识别器。

请参阅文档here