2015-01-28 11 views
2

나는 UIImageView 안에 UIView 안에있는 UIButton의 접촉에 응답하지 않습니다. 나는 이유를 알 수 없다. 나는 원래의 프로그램이하는 일을하면서 코드를 가장 단순한 형태로 분해했다.서브 뷰로 응답하지 않는 UIButton

#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에 추가하는 것입니다.

답변

8

있는 UIImageView은 기본적으로 false로 userInteractionEnabled 유지 - 그것이 작동하는 코드의 행 다음에 추가 할 수 있습니다.

_panel.userInteractionEnabled = true; 
+0

이것이 효과가 있습니다! 고맙습니다. – user1118374

+0

그래서 superview에서 사용자 상호 작용이 사용 중지 된 경우 하위보기도 사용 중지됩니다. – user1118374

+0

네 말이 맞아. –

0

부모보기에서 사용자 상호 작용이 사용 설정되어 있는지 확인하십시오. UIImageView는 기본적으로 userInteractionEnabled가 false입니다.

너희가 서로에 몇 가지 하위 뷰를 추가로
_panel.userInteractionEnabled = true 
+0

답변을 다시 읽었을 때 올바른 생각이지만 잘못된 변수가 있습니다. 이미지보기는 _panelSC가 아닌 _panel입니다. 하지만 당신이 옳았는데, 문제 였고 사용자 상호 작용이 필요했습니다. 고마워. – user1118374

+0

@ user1118374> 지적 해 주셔서 감사합니다. :) – Rashad

2

, 당신의 버튼을 클릭 할 수 있는지 확인하고 다른 서브 뷰는 터치 이벤트를 먹고있다, 전반적인 귀하의 의견은 사용자 조치가 활성화되어 있는지 확인하십시오.

힌트 : 그것은 사용자 상호 작용이 기본적으로 사용하지 않도록 설정해야합니다으로

가 사용 된 이미지 뷰에 약간의 문제가있을 수 있습니다.

사용하여 UIView 대신 또는이 작업을 수행 :

_panelSC.userInteractionEnabled = true; 
+0

답을 다시 읽었을 때 올바른 생각이 들었지만 잘못된 변수가있었습니다. 이미지보기는 _panelSC가 아닌 _panel입니다. 하지만 당신이 옳았는데, 문제 였고 사용자 상호 작용이 필요했습니다. 고마워. – user1118374

관련 문제