繁体   English   中英

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

[英]How to send touch events to superview in iOS?

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

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

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

可以这样做吗?

将视图的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;
}

希望能帮助到你!

最受欢迎的投票解决方案对我来说并不完全有效,事实上它正在向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
    }
}
#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

暂无
暂无

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

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