0

在Xcode中5我创建an iPhone app 5“信牌”,它可以左右拖动:阴影拖动自定义的UIView - 大小复位和阴影消失

app screenshot

的瓦片被实现为Tile类利用Tile.xib(这里​​):

Xcode screenshot

tile.png我无阴影SA小图像:

tile.png

dragged.png是用阴影较大的图像:

dragged.png

后者图像被touchesBeganTile.m显示:

- (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

- (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处设置了断点 - 当拖动开始时,后者被击中。但为什么以及如何阻止呢?

+0

你为什么不在你的手势识别器动作中做视图操作?检查'recognizer.state == UIGestureRecognizerStateBegan'并设置大背景+阴影,然后在'recognizer.state == UIGestureRecognizerStateEnded'或取消时重置它...(不知道常量是否正确) –

+1

我试过已经在我的'dragTile:'方法中,然后增加贴图和显示阴影发生得太迟了:它在拖动开始时发生,而当用户触摸瓦片时发生(之前)。这里是我在GitHub历史上的尝试:https://github.com/afarber/ios-newbie/blob/0dff7843a81328f0af945fb496a83151da4e656e/DragTiles/DragTiles/ViewController.m –

+1

啊我明白了......然后看看选项'cancelsTouchesInView'的手势识别器(负责调用你的'touchesCancelled'方法) - 如果你禁用了这个,那么它不应该再被调用... –

回答

1

作为评价所讨论的,解决问题的办法是向的UIGestureRecognizercancelsTouchesInView属性设置为NO使得touchesCancelled:withEvent:方法并不由识别器调用。

请参阅文档here