UITableViewCell子类,绘制在代码中,动画删除按钮

问题描述:

我正在处理一个自定义的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

所以我会先粘贴代码,然后我会解释:

-(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 :) 
      } 
     } 
    } 

因此,大家可以看到整个动画是手动创建的,所以你可以精确地控制你想要的。 :)

这对我终于工作。在UITableViewCell中

subDrawContentView.contentMode = UIViewContentModeLeft; 

超越控制布局子视图方法

- (void)layoutSubviews { 

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

    [super layoutSubviews]; 
}