4

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

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

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

这可以做到吗?

4

3 回答 3

21

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

希望能帮助到你!

于 2013-07-10T14:44:04.843 回答
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
    }
}
于 2018-02-12T11:19:43.567 回答
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
于 2013-07-11T08:34:15.643 回答