【发布时间】:2025-11-26 03:50:02
【问题描述】:
我的项目有 UIButtons 执行代码块最多需要 1 秒。
我在其他项目中的按钮工作正常,但由于某些奇怪的原因,这个项目出现了令人讨厌的延迟。
按钮是大号,所以需要快速按下: 下面是一个例子:
- (IBAction)phonePressed:(id)sender {
UITapGestureRecognizer *backspaceGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backspace)];
UIButton *one = [[UIButton alloc] initWithFrame:CGRectMake(menuShadowLayer2.frame.origin.x+10, menuShadowLayer2.frame.origin.y+10, 80, 80)];
one.layer.cornerRadius = 40;
one.layer.borderColor = [UIColor lightGrayColor].CGColor;
one.layer.borderWidth = 1;
[one setBackgroundColor:[UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:0.3]];
[one setTitle:@"1" forState:UIControlStateNormal];
[one.titleLabel setTextAlignment:NSTextAlignmentCenter];
[one setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
[one.titleLabel setFont:[UIFont fontWithName:@"STHeitiSC-Light" size:50]];
[container addSubview:one];
[one addGestureRecognizer:tap1];
...
- (void)dailed1 {
numberDailed.text = [numberDailed.text stringByAppendingString:@"1"];
NSLog(@"1");
}
这是另一个使用 UIControlEventTouchUpInside 的按钮,即使不慢也一样慢。
UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake(numberDailed.frame.origin.x, numberDailed.frame.origin.y+200, numberDailed.frame.size.width+40, 110)];
cancel.layer.cornerRadius = 15;
[cancel setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
cancel.clipsToBounds = YES;
[container addSubview:cancel];
[cancel addTarget:self
action:@selector(clearNumber:)
forControlEvents:UIControlEventTouchDown];
...
-(IBAction)clearNumber:(id)sender{
[numberDailed setText:@""];
}
我使用了UITapGestureRecognizer,因为使用按钮UIControlEventTouchUpInside 或TouchDown 更慢。
按下此按钮 (one) 时,我必须等待大约半秒到一秒才能再次点击。当您输入一个数字并且一半的数字尚未添加到字符串中时,这显然会产生挫败感。
我已将UIScrollView 设置为delaysContentTouches = NO;
该按钮立即突出显示。它根本不会立即执行代码。
我以按钮one 为例,但这适用于我项目中的所有UIButtons。
想法?
【问题讨论】:
-
您为什么在
UIButton上使用点击手势?UIButton内置了对处理水龙头的支持。 -
为什么一个按钮有点击手势?你不应该在按钮上添加任何点击手势,因为它已经有一个。您应该使用 UIControlEventTouchUpInside 以及为什么它更慢?请添加更多详细信息。
-
@rmaddy 我通常使用 UIControlEventTouchUpInside,但正如帖子中提到的,它甚至比我的点击手势还要慢。
-
你是在真机上测试还是在使用魔法垫的模拟器上测试?
-
@TejaNandamuri 我希望我知道!我试图调查每一件事来说明为什么它很慢。它在一个 UIView 中,它在一个 UIScrollView 中。视图上没有太多东西,所以我不明白为什么它很慢。
标签: ios objective-c uiscrollview uibutton