带有按钮的 iOS 子视图不响应触摸。

Posted

技术标签:

【中文标题】带有按钮的 iOS 子视图不响应触摸。【英文标题】:iOS Subview with buttons not responding to touches. 【发布时间】:2012-03-01 02:13:23 【问题描述】:

我有一个 UIView 子类,其中有一个带有按钮的框架/图像。我正在尝试将它添加到我的视图控制器中,但是当我这样做时,它会显示出来,但没有一个按钮会注册任何触摸。

这是我的 UIView 子类。

    #import "ControlView.h"

@implementation ControlView

- (id)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self) 
        CGRect slideFrame = CGRectMake(0, 402, 320, 82);
        slider = [[UIView alloc] initWithFrame:slideFrame];
        slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shelf.png"]];
        [self addSubview:slider];

        UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"];
        UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];

        btnImage = [UIImage imageNamed:@"NavBar_01.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];
        btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(12, 28, 70, 50);
        [btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn1];
        [slider bringSubviewToFront:btn1];
        [btn1 addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_02.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"];
        btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(87, 28, 70, 50);
        [btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn2];
        [slider bringSubviewToFront:btn2];
        // [btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_03.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"];
        btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn3.frame = CGRectMake(162, 28, 70, 50);
        [btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn3];
        [slider bringSubviewToFront:btn3];
        // [btn3 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        btnImage = [UIImage imageNamed:@"NavBar_04.png"];
        btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"];
        btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn4.frame = CGRectMake(237, 28, 70, 50);
        [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
        [btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
        [slider addSubview:btn4];
        [slider bringSubviewToFront:btn4];
        // [btn4 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

        UISwipeGestureRecognizer *recognizer;

        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideUp)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
        [self addGestureRecognizer:recognizer];

        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideDown)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
        [self addGestureRecognizer:recognizer];

    
    return self;


-(void)slideUp

    // Slide up based on y axis
    CGRect frame = slider.frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    frame.origin.y = 320;
    slider.frame = frame;
    [UIView commitAnimations];  


-(void)slideDown

    CGRect frame = slider.frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    frame.origin.y = 425;
    slider.frame = frame;
    [UIView commitAnimations];  

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect

    // Drawing code

*/

@end

这就是我将它添加到我的 ViewController 的方式。

 ControlView *cont = [[ControlView alloc]init];
    [homeView.view addSubview:cont];

我尝试将相同的代码添加到 ViewController 和按钮等。所以我确信这是在子视图和 ViewController 之间使用 Delegates/Self 等简单/愚蠢的东西但是我有一个心理障碍。

【问题讨论】:

【参考方案1】:

你在做这个吗?

 ControlView *cont = [[ControlView alloc]init]; 

你应该这样做

ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

【讨论】:

您先生是一位绅士和一位学者。效果很好,我知道这是小事/愚蠢。 实际上,现在我有相反的情况。它添加到的视图不会使用该代码注册触摸。

以上是关于带有按钮的 iOS 子视图不响应触摸。的主要内容,如果未能解决你的问题,请参考以下文章

解除键盘后,带有所选文本的UITextView不响应触摸

UITableView 在滚动时不响应某些触摸

UIButton 不响应触摸事件

在Scroll View的高度动画后,UIScrollView的子视图不响应触摸

iOS 应用程序中的按钮不响应屏幕边缘的触摸

在 ios 的滚动视图内检测按钮的触摸