2011-04-21 60 views
6

我正在處理一個自定義的UITableViewCell子類,其中的一切都是在代碼中繪製而不是使用UILabels等(這是部分學習練習,部分原因是因爲在代碼中繪圖很多我知道這對於一些標籤來說不會產生太大的變化,但最終我會把它推廣到更復雜的單元格中。)UITableViewCell子類,繪製在代碼中,動畫刪除按鈕

目前我正在努力使用刪除按鈕動畫:how進行動畫顯示單元作爲刪除按鈕滑動收縮英寸

image

首先,我繪製了單元格的contentView的自定義子視圖。一切都在這一個子視圖中繪製。

我通過對電池本身醒目layoutSubviews設置子視圖的大小,這樣做的:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    CGRect b = [self.contentView bounds]; 
    [subcontentView setFrame:b];  
} 

我這樣做,而不是隻設置一個自動尺寸面膜,因爲它似乎在測試更加可靠,但我如果需要,可以在測試中使用自動調整掩碼方法。

現在,當有人擊中減號時發生的默認事件是視圖被擠壓。

enter image description here

我可以通過避免這種情況,設置了我的手機,打電話

subcontentView.contentMode = UIViewContentModeRedraw; 

這給了我正確的最終結果的時候(我的自定義視圖與新的大小重繪,並奠定了正確的,就像我發佈的第一張圖片),但過渡的動畫是不愉快的:它看起來像細胞拉伸和縮小。

我知道爲什麼動畫是這樣工作的:核心動畫不會要求您的視圖爲每個幀重繪,它會重新繪製動畫的結束位置,然後插值找到中間位。

另一種解決方案是做

subcontentView.contentMode = UIViewContentModeLeft; 

這只是在我的細胞繪製刪除按鈕,所以它涵蓋了其中的一部分。

enter image description here

如果我也實現了

- (void) didTransitionToState:(UITableViewCellStateMask)state 
{ 
    [self setNeedsDisplay]; 
} 

那麼一旦刪除按鈕在細胞「跳」到正確的規模已經下滑。這樣就沒有好的幻燈片動畫了,但最終我得到了正確的結果。

我想我可以在刪除按鈕出現的同時運行我自己的動畫,臨時創建另一個視圖,其中包含舊視圖的視圖圖像副本,將我的視圖設置爲新大小,並在它們之間淡化 - 這樣就會有一個很好的交叉淡入淡出,而不是一個急劇的跳躍。任何人都使用這種技術?

現在,您可能會問,爲什麼我不能使用contentStretch屬性,並給它一個區域來調整大小。與此相關的問題是我正在做一些合理的通用的事情,所以它並不總是可能的。在這個特定的例子中,它會工作,但更復雜的單元格可能不會。

所以,我的問題(畢竟這個背景)是:你在這種情況下做什麼?有沒有人有動畫刪除按鈕工作自定義繪製單元格?如果沒有,最好的折衷是什麼?

+0

的子類,我得到了工作,雖然我沒有與我有代碼現在。你可以用我的電子郵件發短信給我嗎,所以我不會忘記幫助你嗎? – Peres 2012-01-03 08:33:12

回答

1

所以我會先粘貼代碼,然後我會解釋:

-(void)startCancelAnimation{ 
    [cancelButton setAlpha:0.0f]; 
    [cancelButton setFrame:CGRectMake(320., cancelButton.frame.origin.y, cancelButton.frame.size.width, cancelButton.frame.size.height)]; 
    cancelButton.hidden=NO; 

    [UIView animateWithDuration:0.4 
        animations:^(void){ 
        [progressView setFrame:CGRectMake(progressView.frame.origin.x, progressView.frame.origin.y, 159.0, progressView.frame.size.height)]; 
        [text setFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y, 159.0, text.frame.size.height)]; 
        [cancelButton setFrame:CGRectMake(244., cancelButton.frame.origin.y, cancelButton.frame.size.width, cancelButton.frame.size.height)]; 
         [cancelButton setAlpha:1.0f];  
        } ]; 
} 

-(void)stopCancelAnimation{ 
    [UIView animateWithDuration:0.4 
        animations:^(void){ 
         [cancelButton setFrame:CGRectMake(320., cancelButton.frame.origin.y, cancelButton.frame.size.width, cancelButton.frame.size.height)]; 
         [cancelButton setAlpha:0.0f]; 
        }completion:^(BOOL completion){ 
         cancelButton.hidden=YES; 
         [cancelButton setAlpha:1.0f]; 
         [progressView setFrame:CGRectMake(progressView.frame.origin.x, progressView.frame.origin.y, DEFAULT_WIDTH_PROGRESS, progressView.frame.size.height)]; 
         [text setFrame:CGRectMake(text.frame.origin.x, text.frame.origin.y, DEFAULT_WIDTH_TEXT, text.frame.size.height)]; 
         } 
    ]; 

} 

-(void)decideAnimation{ 
    if([cancelButton isHidden]){ 
     [self startCancelAnimation]; 
    } 
    else{ 
     [self stopCancelAnimation]; 
    } 
} 

所以我有什麼有一個按鈕,看起來像這樣: enter image description here

我有一個IBOutlet它。我正在做的是調整UIProgressViewUITextField(您可以調整任何你想要的大小)。至於代碼非常簡單,但如果您需要任何幫助來了解正在發生的事情,請詢問。另外,不要忘記將SWIP手勢添加到UITableView ...就像這樣:

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; 
    gesture.numberOfTouchesRequired=1; 
    gesture.direction = UISwipeGestureRecognizerDirectionRight; 
    [table addGestureRecognizer:gesture]; 
    [gesture release]; 

最後,做這一切的方法:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { 

     if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
      //Get the cell of the swipe... 
      CGPoint swipeLocation = [gestureRecognizer locationInView:self.table]; 
      NSIndexPath *swipedIndexPath = [self.table indexPathForRowAtPoint:swipeLocation]; 
      UITableViewCell* swipedCell = [self.table cellForRowAtIndexPath:swipedIndexPath]; 
      //Make sure its a cell with content and not an empty one. 
      if([swipedCell isKindOfClass:[AMUploadsTableViewCell class]]){ 
       // It will start the animation here 
       [(AMUploadsTableViewCell*)swipedCell decideAnimation]; 
       // Do what you want :) 
      } 
     } 
    } 

因此,大家可以看到整個動畫是手動創建的,所以你可以精確地控制你想要的。 :)

2

這對我終於工作。在UITableViewCell中

subDrawContentView.contentMode = UIViewContentModeLeft; 

超越控制佈局子視圖方法

- (void)layoutSubviews { 

    CGRect b = [subDrawContentView bounds]; 
    b.size.width = (!self.showingDeleteConfirmation) ? 320 : 300; 
    [subDrawContentView setFrame:b]; 
    [subDrawContentView setNeedsDisplay]; 

    [super layoutSubviews]; 
} 
相關問題