UIView动画最后被取消

时间:2013-12-24 10:14:02

标签: ios objective-c animation uiview background

我的单元格UIView为白色backgroundColor。我想用动画改变背景颜色。我在-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];
}

2 个答案:

答案 0 :(得分:1)

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


  //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];
 }


答案 1 :(得分:0)

我已经解决了我的问题:

- (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];
}