2014-02-12 29 views
0

背景:我的應用程序的 部分是展覽的平面圖是可縮放/滾動且每個地板上的計劃將有一個按鈕疊加在按下時,會顯示信息看臺關於該參展商。自定義的UIButton不是內的UIImageView工作

我到目前爲止所做的事情: 我在故事板上創建了一個UIViewController。當這個UIVC加載時,會以編程方式添加UIScrollView,然後將UIImageView添加到該UIViewView中。然後,我添加了一個UIButton到UIIV上,顯示ok(作爲綠色框),但它的動作選擇器從未被觸發。

代碼:

- (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

默認情況下UIImageViewuserInteractionEnabled設置爲NO。只需將其設置爲YES並且您的UIButton將會收到觸摸事件。

+0

非常好!這工作完美。感謝您的快速和簡潔的答案。 – Ryan