UIButton子类突出显示错误(突出显示在点​​击或触摸后仍然存在或仍然存在)

Mat*_*ros 1 iphone objective-c uibutton ipad ios

UIButton在我的应用程序中进行了子类化,很多时候,即使我按下按钮,高亮颜色也会保留.我无法弄清楚究竟是什么导致这种情况,因为它似乎只是偶然发生,但它似乎发生在大约50%的时间.我非常确定这是可重复的.当我在a中有一个按钮UITableViewCell并且在表视图仍在滚动时我点击它时,我经常会发生这种情况.

我在覆盖setHighlighted子类中的方法的方式有什么问题吗?这是我的实施:

@implementation SCPFormButton

- (id)initWithFrame:(CGRect)frame label:(NSString *)label
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *buttonLabel = [[UILabel alloc] init];
        buttonLabel.attributedText = [[NSAttributedString alloc] initWithString:[label uppercaseString] attributes:kButtonLabelAttributes];
        [buttonLabel sizeToFit];
        buttonLabel.frame = CGRectMake(kMaxWidth / 2 - buttonLabel.frame.size.width / 2, kStandardComponentHeight / 2 - buttonLabel.frame.size.height / 2, buttonLabel.frame.size.width, buttonLabel.frame.size.height);
        [self addSubview:buttonLabel];

        self.backgroundColor = kFormButtonColorDefault;
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted
{
    self.backgroundColor = highlighted ? kFormButtonColorHighlighted : kFormButtonColorDefault;
    [self setNeedsDisplay];
}

@end
Run Code Online (Sandbox Code Playgroud)

ser*_*gio 5

我会试着打电话给supersetHighlighted.的确,Apple为UIControl所说的文档:

如果控件突出显示,请指定YES; 否则没有.默认情况下,不突出显示控件.当跟踪期间触摸进入和退出以及触摸时,UIControl会自动自动设置和清除此状态.

因此,似乎存在某种UIControl与此相关的状态处理.

如果这没有帮助,我会尝试添加日志跟踪,以便您可以检查处理触摸时按钮所处的状态.