2014-05-01 38 views
1

我在这里阅读过多个解决方案,但他们都不会在我的情况下工作。UITapGestureRecognizer不能在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中。 displayNotificationMessage有时用于显示具有mapview的viewcontroller或UITableViewController。你可以说它必须以与UIAlertView相同的方式工作:不管用户在哪个屏幕上总是显示。

有没有人看到错误或什么? 提前致谢!

回答

0

你的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]; 
+0

我评论过'[self addSubview:messageLabel];'没有成功。 – Michael90

0

我刚刚尝试了自定义UIView上的代码,它工作得很好。将UILabel作为子视图不起作用,它反应良好。对我来说,看起来你可能有另一个UIView放在这一个(这使得这个埋没,因此没有响应),或者你的superview注册另一个Tap手势。

+0

我有一个ViewController上注册的UIPanGestureRecognizer。他们会冲突吗? – Michael90

+0

试着评论一下,看看它是否开始工作。 – Michal

+0

不起作用。我也开始想,顶端可能会有另一种观点。但是如果我使用下面这行代码就没有意义:'[[[[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject] addSubview:self];' 还是这样做? – Michael90

相关问题