gpt4 book ai didi

ios - iOS 7 上 UIButton 高亮状态的延迟

转载 作者:可可西里 更新时间:2023-11-01 03:33:05 26 4
gpt4 key购买 nike

我在 Xcode 中创建了新项目 - Single View 应用程序。应用程序只有两个按钮。

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setFrame:CGRectMake(0, self.view.frame.size.height-40-100, self.view.frame.size.width, 40)];
[button1 setTitle:NSLocalizedString(@"button 1", nil) forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundColor:[UIColor greenColor]];
[button2 setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 40)];
[button2 setTitle:NSLocalizedString(@"button 2", nil) forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.view addSubview:button2];

当我在装有 iOS 7 的 iPhone 上运行此应用程序时,当我按下此按钮时,第二个按钮会延迟突出显示状态。在装有 iOS 6 的 iPhone 上,第二个按钮工作完美。

为什么 iOS 7 上的按钮高亮有延迟?

最佳答案

我的问题是我有一个 UIButton 作为分页 UIScrollView 的 subview ,所以我希望用户能够在按钮没有按下按钮。在 iOS6 中,如果你通过圆角矩形按钮执行此操作,它工作正常,但在 iOS7 中它也可以工作,但按钮不会触发它的突出显示。所以为了解决这个问题,我使用 longPressGestureRecognizer 实现了我自己的 UIButton:

- (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
{
if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{

CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];

if (CGRectContainsPoint(self.bounds, touchedPoint))
{
[self addHighlights];
}
else
{
[self removeHighlights];
}
}
else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
if (self.highlightView.superview)
{
[self removeHighlights];
}

CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];

if (CGRectContainsPoint(self.bounds, touchedPoint))
{
if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)])
{
[self.delegate buttonViewDidTouchUpInside:self];
}
}
}
}

然后当您初始化 longPressGestureRecognizer 时,您可以:

self.longPressGestureRecognizer.minimumPressDuration = .05;

这将允许您在按钮上滑动而不触发它,还可以让您按下按钮并触发其突出显示。希望这会有所帮助。

关于ios - iOS 7 上 UIButton 高亮状态的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19900612/

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