iphone – 突出显示自定义UIButton

2019-06-11
我正在构建的应用程序有很多自定义UIButton位于相当精确布局的图像之上.巴顿,控制图像和标签,以及你有什么,但有一个清晰的自定义风格UIButton坐在它上面来处理水龙头.

我的客户昨天说,“我想要点击它时突出显示”.没关系,它立即推动一个新的uinavigationcontroller视图……它没有眨眼,所以他很困惑. Oy公司.

这就是我为解决这个问题所做的工作.我不喜欢它,但这就是我所做的:

我将UIButton(将其命名为FlashingUIButton)进行了子类化.

出于某种原因,我无法在控制模式下突出显示背景图像.它似乎永远不会达到“突出”的状态.不知道为什么会这样.

所以我写道:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setBackgroundImage:[UIImage imageNamed:@"grey_screen"] forState:UIControlStateNormal];
    [self performSelector:@selector(resetImage) withObject:nil afterDelay:0.2];
    [super touchesBegan:touches withEvent:event];
}

-(void)resetImage
{
    [self setBackgroundImage:nil forState:UIControlStateNormal];
}

这快乐地将我的grey_screen.png(一个30%不透明的黑盒子)放在按钮上,当它被点击时,用一个快乐的空虚替换它.2秒后.

这很好,但这意味着我必须通过我的所有笔尖并将我的所有按钮从UIButtons更改为FlashingUIButtons.这不是世界末日,但我真的希望将其作为UIButton类别来解决,并且一举击中所有鸟类.

有没有比这更好的方法的建议?

您可以使用手势识别器拦截触摸事件,然后以编程方式将识别器添加到您的所有uibutton.例如:

//
//  HighlighterGestureRecognizer.h
//  Copyright 2011 PathwaySP. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface HighlightGestureRecognizer : UIGestureRecognizer {
    id *beganButton;
}

@property(nonatomic, assign) id *beganButton;

@end

和实施:

//
//  HighlightGestureRecognizer.m
//  Copyright 2011 PathwaySP. All rights reserved.
//

#import "HighlightGestureRecognizer.h"


@implementation HighlightGestureRecognizer

@synthesize beganButton;

-(id) init{
    if (self = [super init])
    {
        self.cancelsTouchesInView = NO;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.beganButton = [[[event allTouches] anyObject] view];
    if ([beganButton isKindOfClass: [UIButton class]]) {
        [beganButton setBackgroundImage:[UIImage imageNamed:@"grey_screen"] forState:UIControlStateNormal];
        [self performSelector:@selector(resetImage) withObject:nil afterDelay:0.2];

    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)reset
{
}

- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event
{
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
{
    return NO;
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
{
    return NO;
}

- (void)resetImage
{
    [beganButton setBackgroundImage: nil forState:UIControlStateNormal];
}

@end

嗯.我没有测试这段代码,所以如果它不起作用,不要起诉我,只是评论,我们会解决它.我只是快速写了它以供参考.即使它不起作用,逻辑应该仍然是合理的.

编辑:

忘记添加,你将手势识别器添加到按钮的方式如下:

HighlighterGestureRecognizer * tapHighlighter = [[HighlighterGestureRecognizer alloc] init];

[myButton addGestureRecognizer:tapHighlighter];
[tapHighlighter release];

所以基本上你要声明它,初始化它,然后添加它.之后,您将要释放它,因为addGestureRecognizer会保留它.