如何将触摸事件发送到iOS中的超级查看?

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

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

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

可以这样做吗?

将视图的userInteractionEnabled属性设置为NO 。 这让所有人都接触到它背后的观点。

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

 yourView.userInteractionEnabled = NO; 

当你有一个透明的视图,其中有一个button:

创build该视图的子类并覆盖pointInside方法。 我这样做的方法是将button的框架作为一个属性给子类,以检查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; } 

希望能帮助到你!

 #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