【问题标题】:How to send touch events to superview in iOS?如何在iOS中将触摸事件发送到superview?
【发布时间】:2013-07-10 14:07:36
【问题描述】:

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

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

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

这个可以吗?

【问题讨论】:

    标签: ios uiview uiresponder uievent


    【解决方案1】:

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

    希望对你有帮助!

    【讨论】:

    • 没有不起作用,按钮无法点击,因为透明视图包含一个按钮
    • 啊,我误解了你的情况。所以你有一个透明视图,里面有一个按钮,你想点击它,但是在透明部分,触摸必须通过?我已经用我这样做的方式更新了我的答案。
    • 我和你说的一样不工作只是看看下面的代码不工作
    • 查看代码一次,我是否正确,我只能点击透明视图上的按钮,但不能点击主视图按钮
    • 既然CGRectContainsPoint()返回的是BOOL,那写return CGRectContainsPoint(_buttonFrame, point)作为方法体不是更简单吗?
    【解决方案2】:

    票选最高的解决方案对我来说并不完全有效,它实际上是将触摸传递给 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
        }
    }
    

    【讨论】:

      【解决方案3】:
      #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
      

      【讨论】:

        猜你喜欢
        • 2019-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-11
        • 2014-01-01
        • 2016-07-20
        相关资源
        最近更新 更多