UIButton作为子视图没有响应

时间:2015-01-28 05:16:44

标签: ios objective-c uiview uibutton

我在UIButtonUIViewUIImageView没有响应触摸。我无法弄清楚原因。我已经将代码分解为最简单的形式,同时仍然执行我的原始程序。

#import "TestVideoViewController.h"

@interface TestVideoViewController ()

@property (strong, nonatomic)UIImageView *panel;
@property (strong, nonatomic)UIView *panelSC;
@property (strong, nonatomic)UIButton *panelButtonSC;
@property (strong, nonatomic)UIButton *videoButton;

@end

@implementation TestVideoViewController

-(void) viewDidLoad {

    _panelButtonSC  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_panelButtonSC addTarget:self action:@selector(buttonPressedSC:) forControlEvents:UIControlEventTouchUpInside];
    [_panelButtonSC setTitle:@"Open" forState:UIControlStateNormal];
    _panelButtonSC.frame = CGRectMake(511, 701, 66, 67);

    _panel = [[UIImageView alloc] initWithFrame:CGRectMake(100, 768, 824, 600)];
    _panel.backgroundColor = [UIColor whiteColor];

    _panelSC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 784, 560)];
    _panelSC.backgroundColor = [UIColor whiteColor];

    _videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_videoButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    [_videoButton setTitle:@"Play" forState:UIControlStateNormal];
    [_videoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _videoButton.frame = CGRectMake(400, 400, 200, 40);

    [_panelSC addSubview:_videoButton];

    [_panel addSubview:_panelSC];

    [self.view addSubview:_panel];
    [self.view addSubview:_panelButtonSC];

}

- (IBAction)buttonPressedSC:(UIButton*)sender {

    [self animatePanelUp];
}

- (IBAction)playVideo:(UIButton*)sender {

    NSLog(@"play video");
}

- (void) animatePanelUp {
    [UIView animateWithDuration: 0.5
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         _panel.frame = CGRectMake(47, 166, 929, 602);
                     }
                     completion:nil];

}

@end

我在这里看了每个类似的答案。据我所知,我所有的框架都是正确的。我必须有一些简单而愚蠢的东西,以至于我失踪了。为什么" playVideo"方法永远火了?它唯一有效的时间是我将按钮直接添加到self.view

3 个答案:

答案 0 :(得分:8)

UIImageView默认情况下将userInteractionEnabled保持为false - 添加以下代码行。

_panel.userInteractionEnabled = true;

答案 1 :(得分:2)

当您在彼此添加一些子视图时,请检查您的按钮是否可点击,并且没有其他子视图占用触摸事件,并且总体上确保您的视图已启用用户操作。

<强>提示:

使用的imageview可能存在一些问题,因为默认情况下会禁用用户交互。

使用UIView代替或执行此操作:

_panelSC.userInteractionEnabled = true;

答案 2 :(得分:0)

确保已启用父视图的用户交互。默认情况下,UIImageView的userInteractionEnabled为false。

_panel.userInteractionEnabled = true