2014-02-12 2 views
0

배경 : 내 앱의 일부는 확대/축소가 가능한 전시 평면도이며 평면도의 각 스탠드에는 버튼 오버레이가있어 정보를 표시합니다 그 출품자에 대해서.사용자 정의 UIButton이 UIImageView 내에서 작동하지 않습니다.

내가 지금까지 한 것 : 스토리 보드에 UIViewController를 만들었습니다. 이 UIVC가로드되면 UIScrollView가 프로그래밍 방식으로 추가되고 UIImageView가 추가됩니다. 그런 다음 확인 (녹색 상자)을 표시하는 UIIV에 UIButton을 추가했지만 액션 선택기는 절대로 실행되지 않습니다.

코드 : 기본적 UIImageView으로

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDirectory = [docPaths objectAtIndex:0]; 
    NSString *filePath = [NSString stringWithFormat:@"%@/nia_floorplan.png", docDirectory]; 
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath]; 
    _imageView = [[UIImageView alloc] initWithImage:image]; 

    CGFloat screenWidth = self.view.frame.size.width; 
    CGFloat screenHeight = self.view.frame.size.height; 
    CGFloat scrollViewWidth = (screenWidth * 0.9); 
    CGFloat scrollViewHeight = (screenHeight * 0.83); 
    CGFloat scrollViewLeft = (screenWidth * 0.05); 
    CGFloat scrollViewTop = (screenHeight * 0.03); 

    self.scrollView =[[UIScrollView alloc] initWithFrame:CGRectMake(loginOverlayLeft, loginOverlayTop, loginOverlayWidth, loginOverlayHeight)]; 

    [self buildHotspots]; 
    [_scrollView addSubview:_imageView]; 

    [_scrollView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.7f]]; 
    [_scrollView setMaximumZoomScale:3.5]; 
    [_scrollView setClipsToBounds:YES]; 
    [_scrollView setMinimumZoomScale:1.0]; 
    [_scrollView setDelegate:self]; 
    [_scrollView setBounces:NO]; 
    [_scrollView setContentSize:[image size]]; 

    [self.view addSubview:_scrollView]; 
    [self.view bringSubviewToFront:testBtn]; 

} 

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return _imageView; 
} 

-(void)buildHotspots { 
    testBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    testBtn.frame = CGRectMake(364, 267, 37, 61); 
    [testBtn setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.7f]]; 
    [testBtn addTarget:self action:@selector(displayInfo) forControlEvents:UIControlEventTouchUpInside]; 
    [_imageView addSubview:testBtn]; 
} 

-(void)displayInfo{ 
    NSLog(@"Button pressed"); 
} 

답변

4

NO-userInteractionEnabled 세트가 있습니다. YES으로 설정하면 UIButton에 터치 이벤트가 전송됩니다.

+0

우수! 이것은 완벽하게 작동합니다. 신속하고 간결한 답변 주셔서 감사합니다. – Ryan

관련 문제