gpt4 book ai didi

ios - UIButton 只检测上半部分的触摸

转载 作者:行者123 更新时间:2023-11-28 22:28:45 25 4
gpt4 key购买 nike

我正在向一个 UIView 添加 3 个 UIButton,通常只有其中的 1 到 2 个会检测到触摸,即便如此,它也只会检测到按钮的上半部分。有人知道为什么会这样吗?

for (int i = 0; i <= 2; i++) {
int randIndex = arc4random() % [images count];
// UIImage *randImage = [images objectAtIndex:randIndex];
NSString *number = [numbers objectAtIndex:randIndex];
[images removeObjectAtIndex:randIndex];
[numbers removeObjectAtIndex:randIndex];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// [button setImage:randImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpOutside];
button.frame = CGRectMake(0, 8, 108, 40);
button.tag = [number integerValue];
for (UIView *sub in [baseone subviews]) {
CGPoint topRight = CGPointMake(sub.frame.origin.x + sub.frame.size.width, sub.frame.origin.y);
button.frame = CGRectMake(topRight.x, 8, 108, 40);
}
[baseone addSubview:button];
}

最佳答案

baseone 的宽度可能只够容纳前两个按钮。你看到第三个按钮的触地选择了吗?

另外,你的控制事件是TouchUpOutside,我经常看到TouchUpInside,是故意的吗?

PS 没有解决你的问题,但是这个循环:

for (UIView *sub in [baseone subviews]) {
CGPoint topRight = CGPointMake(sub.frame.origin.x + sub.frame.size.width, sub.frame.origin.y);
button.frame = CGRectMake(topRight.x, 8, 108, 40);
}

可以简化为:

UIView * sub = [[baseone subviews] lastObject]
button.frame = CGRectMake(sub.frame.origin.x+sub.frame.size.width, 8, 108, 40);

因为最后一个 subview 总是决定下一个按钮的框架。

关于ios - UIButton 只检测上半部分的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18114914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com