我已经将UILabel和UIButton添加到UICollectionViewCell,但是当触摸/点击单元格时,它只会在单元格的左半部分注册我的水龙头。忽略单元右半部分的任何触摸/点击?我还应该注意,文本在UILabel中的持续时间并不重要。
示例文字:
@"Short text"
@"Very long text does not matter"
在上面的文字中,触摸停止了“文本”中最后一个“t”的位置。
CODE:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES;
myLabel = [[UILabel alloc] initWithFrame:self.bounds];
myLabel.frame = UIEdgeInsetsInsetRect(myLabel.frame, UIEdgeInsetsMake(0, SEARCH_CELL_PADDING, 0, SEARCH_CELL_PADDING +30));
myLabel.textAlignment = NSTextAlignmentLeft;
myLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
myLabel.font = RECENT_SEARCH_CELL_FONT;
myLabel.textColor = WK_COLOR_GRAY_77;
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 30)];
myButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *btnImage = [UIImage imageNamed:@"icon2.png"];
[myButton setImage:btnImage forState:UIControlStateNormal];
UIImage *btnSelectedImage = [UIImage imageNamed:@"icon.png"];
[myButton setImage:btnSelectedImage forState:UIControlStateSelected];
[myButton setUserInteractionEnabled:NO];
[self addSubview:myButton];
[self addSubview:myLabel];
}
return self;
}
可能它是单元格中的偏移量还是uicollectionview设置中的偏移量?
更新:
我添加了以下代码,tap方法永远不会被调用。
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
[_recentSearchLabel addGestureRecognizer:tapGesture];
我还将上面的代码更改为以下内容:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES;
myLabel = [[UILabel alloc] initWithFrame:self.bounds];
myLabel.frame = UIEdgeInsetsInsetRect(myLabel.frame, UIEdgeInsetsMake(0, SEARCH_CELL_PADDING, 0, SEARCH_CELL_PADDING +30));
myLabel.textAlignment = NSTextAlignmentLeft;
myLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
myLabel.font = RECENT_SEARCH_CELL_FONT;
myLabel.textColor = WK_COLOR_GRAY_77;
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 30)];
myButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *btnImage = [UIImage imageNamed:@"icon2.png"];
[myButton setImage:btnImage forState:UIControlStateNormal];
UIImage *btnSelectedImage = [UIImage imageNamed:@"icon.png"];
[myButton setImage:btnSelectedImage forState:UIControlStateSelected];
[myButton setUserInteractionEnabled:YES];
[self addSubview:myLabel];
[self addSubview:myButton];
}
return self;
}
仍然不正在工作
答案 0 :(得分:0)
替换上面的代码: [self addSubview:myButton]; [self addSubview:myLabel];
使用: [self addSubview:myLabel]; [self addSubview:myButton];
由于您要将视图添加到视图作为子视图,因此将该标签添加为可能与按钮位置重叠的子视图。所以首先添加"标签"作为子视图,然后是"按钮"。 Buton现在会采取行动。
希望这能解决您的问题。 。欢呼:)
答案 1 :(得分:0)
问题在于收集视图不够广泛,但不知何故,细胞在collectionviews视图中拍摄了一半。所以细胞在收集视图上有一半,一半是关闭的。关闭的一半没有认识到接触。