如何在iOS中向superview发送触摸事件?

时间:2013-07-10 14:07:37

标签: ios uiview uiresponder uievent

我正在做一个iPad应用程序。如何将触摸事件发送到其超级视图?

我正在添加一个始终是其他活动的视图。在那我我将scrollview添加到屏幕的一半,在另一半我添加另一个视图。一切正常,现在我想添加一个按钮和一个小的描述视图,当按钮被点击时会出现,再次点击按钮时会再次消失。为了实现这一点,我采用了一个覆盖整个视图并使其背景颜色清晰的视图。我将此按钮和描述视图放在此视图中,但由于透明视图,它不会滚动。

我想让这个透明视图忽略它的事件。

可以这样做吗?

3 个答案:

答案 0 :(得分:20)

将视图的userInteractionEnabled属性设置为NO。这样可以让所有人触摸到它后面的视图。

您可以在属性检查器的界面构建器/故事板中或代码中设置它:

yourView.userInteractionEnabled = NO;

当您有一个带有按钮的透明视图时:

创建该视图的子类并覆盖pointInside方法。我这样做的方法是将按钮的框架作为属性提供给子类,以便在pointInside方法中进行检查:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL pointInside = NO;
    // Check if the touch is in the button's frame
    if (CGRectContainsPoint(_buttonFrame, point)) pointInside = YES;

    return pointInside;
}

希望它有所帮助!

答案 1 :(得分:1)

最受欢迎的投票解决方案对我来说并不完全有效,事实上它正在向UI的某些部分传递,但是它正在弄乱我的tableView拦截触摸事件,最终它是什么覆盖hitTest 在视图中我想忽略触摸(这将是您的透明视图)并让该视图的子视图处理它们(这将是您的按钮)

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == self) {
        return nil; //avoid delivering touch events to the container view (self)
    }
    else{
        return view; //the subviews will still receive touch events
    }
}

答案 2 :(得分:0)

#import <UIKit/UIKit.h>

@interface TransperentView : UIView

@end

#import "TransperentView.h"

@implementation TransperentView

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) 
  {
    // Initialization code
       [self addTransperentView];
       [self addbutton];     
  }
  return self;
}

-(void)addTransperentView
{

    UIView *aView = [[UIView alloc]initWithFrame:[self.superview bounds]];
    aView.backgroundColor = [UIColor clearColor];   
    [self addSubview:aView];
}

-(void)addbutton
{
   UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [aButton setTitle:@"clickMe" forState:UIControlStateNormal];
   [aButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
   [aButton setFrame:CGRectMake(100, 200, 90, 90)];
   [self addSubview:aButton];     
}

-(void)buttonClicked
{
    NSLog(@"button clicked");
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
  for(UIView *vi in self.subviews)
    {
       if([vi isKindOfClass:[UIButton class]])
        {
            return YES;
        }
    }        
    return NO;
}

//in main view i have added  a button through xib 

@interface ViewController : UIViewController
{    
    IBOutlet UIButton *helloButton;   
}

- (IBAction)whenButtonTapped:(id)sender;

@end


#import "ViewController.h"
#import "TransperentView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{

   [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.     
    TransperentView *tView = [[TransperentView alloc]initWithFrame:self.view.bounds];

    [self.view addSubview:tView];  
}

- (void)didReceiveMemoryWarning
{  
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.   
}

- (void)dealloc
 {
    [helloButton release];
    [super dealloc];
 }

- (IBAction)whenButtonTapped:(id)sender
{     
    [helloButton setTitle:@"world" forState:UIControlStateNormal];   
}

@end