繁体   English   中英

UITapGestureRecognizer在UIView上不起作用

[英]UITapGestureRecognizer not working on UIView

我在这里已经阅读了多种解决方案,但是对于我来说它们似乎都不起作用。

我有一个类NotificationBar:.h文件:

@interface NotificationBar : UIView <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UILabel *messageLabel;

- (id)initWithFrame:(CGRect)frame message:(NSString *)message;

- (void) show:(int)duration;

@end

.m文件:

#import "NotificationBar.h"

@implementation NotificationBar

@synthesize messageLabel;

- (id)initWithFrame:(CGRect)frame message:(NSString *)message
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.alpha = 0;
        self.backgroundColor = [UIColor cabmanBlue];
        self.userInteractionEnabled = YES;

        self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, 0 , self.bounds.size.width - 10, self.bounds.size.height)];
        [self.messageLabel setBackgroundColor:[UIColor clearColor]];
        [self.messageLabel setTextColor:[UIColor whiteColor]];
        [self.messageLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [self.messageLabel setNumberOfLines:2];
        self.messageLabel.text = message;
        [self addSubview:messageLabel];

        UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
        tapRec.delegate = self;
        tapRec.cancelsTouchesInView = NO;
        [self addGestureRecognizer:tapRec];
    }
    return self;
}

- (void) show:(int)duration
{
    [[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] addSubview:self];
    [UIView animateWithDuration:0.5 animations:^{ self.alpha = 1; } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:duration options:UIViewAnimationOptionCurveLinear animations:^{ self.alpha = 0;} completion:^(BOOL finished)
         {
             if(finished) [self removeFromSuperview];
         }];
    }];
}

- (void) dismiss:(UITapGestureRecognizer *)gesture
{
    [self.layer removeAllAnimations];
    [UIView animateWithDuration:0.5 animations:^{ self.alpha = 0; } completion:^(BOOL finished) {
        if(finished)
        {
            [self removeFromSuperview];
        }
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

@end

我通过以下方式使用该类:

- (void) displayNotificationMessage:(NSString *)message withSound:(BOOL) withSound
{
    UIView *topView = [self.window.subviews lastObject];
    [[[NotificationBar alloc] initWithFrame:CGRectMake(topView.bounds.origin.x,
                                                                             64,
                                                                             topView.bounds.size.width,
                                                                             40)
                            message:message] show:10];

    if(withSound) AudioServicesPlaySystemSound(1007);
}

该视图始终显示和显示。 解除功能没有触发,似乎没有任何反应。 displayNotificationMessage放置在AppDelegate中。 当显示带有mapview的viewcontroller或在UITableViewController中时,有时会使用displayNotificationMessage。 您可以说它的工作方式必须与UIAlertView相同:无论用户在哪个屏幕上,始终显示。

有人看到错误或其他东西吗? 预先感谢!

您的UILabel几乎接管了整个视图范围:

self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.origin.x + 5, 0 , self.bounds.size.width - 10, self.bounds.size.height)];

您可以尝试减小其大小,然后查看该手势是否在其他位置有效,或者可以尝试将手势添加到标签中

[self.messageLabel addGestureRecognizer:singleTap];  

我只是在自定义UIView上尝试过您的代码,所以效果很好。 将UILabel作为子视图无效,它的响应很好。 对我来说,它看起来像你可能有任何其他UIView放完了这一个(这使得这个埋藏,因此反应迟钝),或者你有另一个点触手势在您注册的superview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM