iphone - 无法使用 TapGestureRecognizer 突出显示 UIButton

标签 iphone xcode uibutton

我已经设置了一个点击手势识别器并将识别器添加到 uibutton。该按钮有一个背景图像。当我点击按钮时,它根本不突出显示,我唯一能做的就是更改它的 alpha 值。

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
     singleTap.cancelsTouchesInView = NO;

    [btnNext addGestureRecognizer:singleTap];


- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];
NSLog(@"Touch event view: %@",[tappedView class]);
UIButton *myButton = (UIButton *) tappedView;
[self highlightButton:myButton];
tappedView.alpha = 0.5f;

}

任何人都将不胜感激。谢谢

最佳答案

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

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

#import <Foundation/Foundation.h>


@interface HighlightGestureRecognizer : UIGestureRecognizer {
    id *beganButton;
}

@property(nonatomic, assign) id *beganButton;

@end
and the implementation:

//
//  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 会保留它。

也简单尝试一下

adjustsImageWhenHighlighted = YES 在您的按钮上设置了吗?默认值为 YES,但也许您在 xib 中更改了它。这是属性检查器中的“突出显示调整图像”复选框:

enter image description here

关于iphone - 无法使用 TapGestureRecognizer 突出显示 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870953/

相关文章:

ios - 测试在 Xcode 的问题导航器中消失

ios - Xcode 8 内存图 malloc 堆栈无法加载回溯

ios - Swift - 在横向时创建 UIButton

ios - iPhone SE 中按钮不显示全文

iphone - 三层Pan Gesture Recogniser的困惑

触摸屏幕时 ios 应用程序崩溃

ios - 属性值不更新 xcode 中 .plist 文件的后期编辑

iphone - Objective - C 如何使用 View Controller iphone 管理多个 View

ios - 从控制台 XCode iOS 应用程序(不是命令行应用程序)读取

ios - OnTouchUp 和 OnTouchDown 上的 Swift UIButton 背景更改?