在UITableview上闪烁背景

时间:2013-12-10 22:46:44

标签: ios objective-c 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];
}

}

抱歉我的英语不好。感谢您的关注。

2 个答案:

答案 0 :(得分:3)

您可以使用backgroundColor将背景视图添加到UITableViewCell中。然后在你的tableView:didSelectRowAtIndexPath中:你可以使用UIView中的动画API为隐藏或alpha设置动画。动画结束后,您可以将隐藏设置为TRUE或alpha设置为0以获得闪光效果。

答案 1 :(得分: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;
            }];

        }];