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