UIButton 不在 UIScrollView 中交互

发布于 2024-10-04 06:35:31 字数 2973 浏览 4 评论 0原文

我有一个窗口(例如不是全屏)UIScrollView(在UIView内),它使用UIButtons滚动浏览UIImageViews 在它们上(想法是您单击按钮对显示的图像执行某些操作)。 UIButton 确实接受任何触摸事件 - 我该如何修复它?

我已阅读这个这个这个 - 但我要么不理解答案或其含义,要么它并不真正相关。

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

I have a windowed (e.g. not a full-screen) UIScrollView (inside a UIView) which scrolls through groups of UIImageViews with UIButtons on them (the idea being you click the button to do something with the displayed image). The UIButton does take any touch events - how can I fix it?

I've read this, this, this, this and this - but I either don't understand the answer or its implications, or it's not really relevant.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2024-10-11 06:35:31

尝试

[imageView setUserInteractionEnabled:YES]

try

[imageView setUserInteractionEnabled:YES]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文