我有1个tabelview,在单元格中添加4个图像视图。当用户点击任何图像视图时,背景将变暗,刚刚选择的图像视图将显示在昏暗视图的顶部。
我试过这段代码:
self.dimView = [[UIView alloc]initWithFrame:self.view.frame];
self.dimView.backgroundColor = [UIColor blackColor];
self.dimView.alpha = 0.7;
self.dimView.hidden = YES;
[self.sellItemtbl addSubview:self.dimView];
//[self.view bringSubviewToFront:dimView];
//add tap gesture to dimview
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.dimView addGestureRecognizer:singleFingerTap];
但它只创建了1个暗淡视图,而刚刚选择的图像无法显示此视图。我怎样才能做到这一点?请给我一些建议。提前谢谢。
答案 0 :(得分:0)
背景并不清楚你的意思是你的表视图单元格还是整个视图。我假设它只是表视图单元格,并包含表格视图单元格的代码。您可以使用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法将其扩展到整个视图。
- (void)awakeFromNib
{
[self setSelectionStyle:UITableViewCellEditingStyleNone];
[self.contentView setBackgroundColor:[UIColor colorWithRed:1.f green:0.f blue:0.f alpha:1.f]];
[self.imgView1 setUserInteractionEnabled:YES];
[self.imgView2 setUserInteractionEnabled:YES];
[self.imgView3 setUserInteractionEnabled:YES];
[self.imgView4 setUserInteractionEnabled:YES];
[self.imgView1 setTag:1];
[self.imgView2 setTag:2];
[self.imgView3 setTag:3];
[self.imgView4 setTag:4];
[self.imgView1 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView2 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView3 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView4 addGestureRecognizer:[self getTapGestureRecognizer]];
}
- (UITapGestureRecognizer *)getTapGestureRecognizer
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMade:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.cancelsTouchesInView = NO;
return tapGesture;
}
- (void)tapMade:(UITapGestureRecognizer *)recognizer
{
UIImageView *imgView = (UIImageView *)recognizer.view;
NSInteger tag = imgView.tag;
[self.contentView setBackgroundColor:[UIColor colorWithRed:1.f green:0.f blue:0.f alpha:0.7f]];
[self.imgView1 setAlpha:(tag == 1)? 1.f: 0.7f];
[self.imgView2 setAlpha:(tag == 2)? 1.f: 0.7f];
[self.imgView3 setAlpha:(tag == 3)? 1.f: 0.7f];
[self.imgView4 setAlpha:(tag == 4)? 1.f: 0.7f];
}
如果您有任何疑问,请告诉我。