2010-11-27 68 views
0

我有一個窗口(例如不是全屏)UIScrollView(在UIView內),其中滾動UIImageViews組與UIButtons對他們(想法是你點擊按鈕做一些事情顯示的圖像)。 UIButton確實採取任何觸摸事件 - 我該如何解決它?UIButton不會在UIScrollView中互動

我讀過thisthisthisthisthis - 但是我要麼不理解答案或它的含義,或者它不是真正相關。

NSUInteger i; 
for (i = 1; i <= kNumImages; i++) 
{ 
    Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i]; 
    //NSLog(@"testSentence: %@", testSentence); 

    //NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i]; 
    //Your going to need to optimise this by creating another thumbnail image to use here. 

    NSArray *paths  = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]]; 
    //NSLog(@"imageName: %@", imageName); 

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName]; 
    //NSLog(@"image: %@", image); 

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    //NSLog(@"imageView: %@", imageView); 

    [imageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [imageView setBackgroundColor:[UIColor blackColor]]; 

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect rect = imageView.frame; 
    rect.size.height = kScrollObjHeight; 
    rect.size.width = kScrollObjWidth; 
    imageView.frame = rect; 
    imageView.tag = i; // tag our images for later use when we place them in serial fashion 

    imageView.frame = rect; 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    [aButton setTitle:@"Play" forState:UIControlStateNormal]; 
    aButton.frame = CGRectMake(10, 10, 70, 70); 
    [aButton setUserInteractionEnabled:YES]; 
    [aButton setEnabled:YES]; 
    [aButton setAlpha:1]; 

    UIView *buttonWrapperUIView = [[UIView alloc] init]; 

    [aButton addTarget:self action:@selector(clickPlay:) forControlEvents:UIControlEventTouchUpInside]; 

    [imageView addSubview:aButton]; 
    [imageView bringSubviewToFront:aButton]; 

    [scrollView1 addSubview:imageView]; 

    NSLog(@"aButton %@", aButton); 

    [image release]; 
    [imageView release]; 
} 

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview 

回答

4

嘗試[imageView setUserInteractionEnabled:YES]

+0

咄。我不敢相信我沒有嘗試過。謝謝! – glenstorey 2010-11-27 05:19:42