2013-12-10 75 views
0

我正在嘗試測驗程序。我曾用UITableview獲得答案。當用戶點擊正確的答案時,我怎麼能在背景上閃爍?可能嗎?在UITableview上閃爍的背景

這是我的項目的一部分;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]; 

//cell=[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"Cell"]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
} 


NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 
if ([selectedCells containsObject:rowNsNum] ) 
{ 
    //cell.accessoryType = UITableViewCellAccessoryCheckmark; 

} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

NSString *text = [answers objectAtIndex:indexPath.row]; 
NSData *data = [text dataUsingEncoding: [NSString defaultCStringEncoding] ]; 
cell.textLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ]; 

//cell.textLabel.text=[answers objectAtIndex:indexPath.row]; 
cell.textLabel.backgroundColor = [UIColor clearColor]; 
tableView.backgroundColor=[UIColor whiteColor]; 
cell.backgroundColor=[UIColor whiteColor]; 

[cell.textLabel setFont:[UIFont fontWithName:@"Helvatica" size:15]]; 
return cell; 
} 


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

CGRect rect = cell.frame; 
UIView * view = [[UIView alloc] init]; 
UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ListGray"] ]; 

BOOL selx; 
if (answerType==2) 
    selx = [self isSelected:indexPath]; 
else 
    selx = [lastIndexPath isEqual:indexPath]; 

if (selx) { 
    back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"aaaa.png"] ]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    [view addSubview:back]; 
    cell.backgroundView = view; 

    back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 
} else { 
    cell.accessoryView = nil; 
    if (lastIndexPath != nil){ 
     cell.textLabel.textColor = [UIColor blackColor]; 
     [view addSubview:back]; 
     cell.backgroundView = view; 

     back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 
     back = nil; 


    } 
} 
[view addSubview:back]; 
cell.backgroundView = view; 

back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 

} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

tableView.allowsSelection = NO; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 

if (answerType==3) { 
     NSString *secilenAnswerID; 
     secilenAnswerID= [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]]; 

     for (int a=0; a<[answers count]; a++) { 

      cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:a inSection:0]]; 
      cell. accessoryType= UITableViewCellAccessoryNone; 
      [selectedCells removeObject:rowNsNum]; 
      selectedCells=[[NSMutableArray alloc]init]; 
     } 
     cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]; 
     isRadioSelected=TRUE; 
     radioAnswer = [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]]; 
     [selectedCells addObject:rowNsNum]; 


    lastIndexPath = indexPath; 
    [tableView reloadData]; 
} 

} 

對不起,我的英語不好。感謝您的關注。

回答

3

您可以使用backgroundColor將背景視圖添加到您的UITableViewCell中。然後在您的tableView:didSelectRowAtIndexPath中:您可以使用UIView中的動畫API對隱藏或alpha進行動畫製作。動畫完成後,可以將隱藏動畫設置爲TRUE或將alpha恢復爲0以獲得閃光效果。

+0

+1,優雅和簡單。我就是這麼做的。 – Alex

+0

是的你對:)我不知道動畫方法。你能寫一個簡單的例子嗎? –

+0

非常感謝您的建議 –

2

這是我的問題的解決方案。我從斯蒂芬約翰遜的建議:)

[UIView animateWithDuration:0.1f delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{ 
      [UIView setAnimationRepeatCount:3]; 
      cell.alpha = 0; 
     } completion: ^(BOOL finished) { 
      cell.hidden = YES; 

      [UIView animateWithDuration:0.1 animations:^{ 
       cell.alpha = 1; 
      } completion: ^(BOOL finished) { 
       cell.hidden = NO; 
      }]; 

     }];