UIView 动画在最后被取消



我有一个带有白色backgroundColor UIView的单元格。我想用动画改变背景颜色。我在-tableView:tableView didSelectRowAtIndexPath:indexPath中使用此方法:

 [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
                ([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
            } completion:NULL];

动画正确开始,但在结束时,UIView再次变为白色。

我该如何解决这个问题?

编辑:完整代码

#pragma mark - Table view
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arFiles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RecentCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    ((UILabel*)[cell viewWithTag:2]).text = [[_arFiles objectAtIndex:indexPath.row]objectForKey:KEY_FILE_NAME];
   cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 10);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =  ((UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath]);
    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
        ([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
    } completion:NULL];
}

子类化单元格可能按预期工作,听到是代码在自定义单元格中尝试做如下操作

  //in CustomCell.m 

  #import "CustomCell.h"
  @implementation CustomCell
  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
     if (self) {
     UIView *view = [[UIView alloc]initWithFrame:CGRectZero];//your view
      view.tag = 100;
     [self addSubview:view];//add to cell
   }
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];
   UIView *view = [self viewWithTag:100];
    if(selected)
    {
     //animation when selected
     [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){      
        view.backgroundColor = [UIColor orangeColor];
       } completion:NULL];     
   }
   else
   {
     //this is for when deselect if u want color in same sate then set it   
   }

    // Configure the view for the selected state
 }
 - (void)layoutSubviews
 {
    [super layoutSubviews];
    UIView *view = [self viewWithTag:100];//get the view
    view.frame = CGRectMake(5, 5, 100, 30);//setting the frame
 }

在您的控制器中,只需使用此自定义单元格

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if(cell == nil)
        {
          cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
       return cell;
 }
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
  //dont add any animation hear becz u already added in custom cell     

     //  [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
    //       ([cell viewWithTag:100]).backgroundColor = [UIColor orangeColor];
    //    } completion:NULL];
 }

我已经解决了我的问题:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    UITableViewCell *cell =  ((UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath]);
    [self performSelector:@selector(launchAnimation:) withObject:cell afterDelay:0.1]; 
}
-(void)launchAnimation:(UITableViewCell*)cell{
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
        ([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
    } completion:NULL];
}

相关内容

最新更新