简体   繁体   English

将UIButtons添加为子视图时,UIScrollView不滚动

[英]UIScrollView not scrolling when adding UIButtons as subviews

I'm trying to build a simple UIScrollView with paging to horizontally scroll between 3 images. 我正在尝试使用分页构建一个简单的UIScrollView,以在3张图像之间水平滚动。 The tricky part is that I would like that each image would be clickable and catch the click event. 棘手的部分是,我希望每个图像都是可单击的并捕获单击事件。

My technique is to create 3 UIButtons that each consists UIImage. 我的技术是创建3个UIButton,每个UIButton都包含UIImage。 give each button a tag and set an action. 给每个按钮一个标签并设置一个动作。

Problem: I can catch the click event - BUT it's not scrollable! 问题:我可以捕获点击事件- 但是它不可滚动!

Here is my code: 这是我的代码:

- (void) viewDidAppear:(BOOL)animated {

    _imageArray = [[NSArray alloc] initWithObjects:@"content_01.png", @"content_02.png", @"content_03.png", nil];

    for (int i = 0; i < [_imageArray count]; i++) {
        //We'll create an imageView object in every 'page' of our scrollView.
        CGRect frame;
        frame.origin.x = _contentScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = _contentScrollView.frame.size;

        //
        //get the image to use, however you want
        UIImage* image = [UIImage imageNamed:[_imageArray objectAtIndex:i]];

        UIButton* button = [[UIButton alloc] initWithFrame:frame];

        //set the button states you want the image to show up for
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];

        //create the touch event target, i am calling the 'productImagePressed' method
        [button addTarget:self action:@selector(imagePressed:)
         forControlEvents:UIControlEventTouchUpInside];
        //i set the tag to the image #, i was looking though an array of them
        button.tag = i;

        [_contentScrollView addSubview:button];
    }

    //Set the content size of our scrollview according to the total width of our imageView objects.
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * [_imageArray count], _contentScrollView.frame.size.height);

    _contentScrollView.backgroundColor = [ENGAppDelegate backgroundColor];
    _contentScrollView.delegate = self;
}

Well, since UIButton is an UIControl subclass, it "eats up" the touches of your scroll view: 好吧,由于UIButtonUIControl子类,因此它“吃掉了”滚动视图的触感:

[UIScrollView touchesShouldCancelInContentView:] The default returned value is YES if view is not a UIControl object; [UIScrollView touchesShouldCancelInContentView:]如果view不是UIControl对象,则默认返回值为YES。 otherwise, it returns NO. 否则,返回NO。

(from https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instm/UIScrollView/touchesShouldCancelInContentView :) (来自https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instm/UIScrollView/touchesShouldCancelInContentView :)

You could influence this by subclassing UIScrollView and overwriting touchesShouldCancelInContentView: (and/or touchesShouldBegin:withEvent:inContentView: ). 可以通过子类化UIScrollView并覆盖touchesShouldCancelInContentView:和/或touchesShouldBegin:withEvent:inContentView: 影响此效果。 However, for your use case I'd not use buttons in the first place. 但是,对于您的用例,我不会首先使用按钮。 Why not just add a tap gesture recognizer to the scrollview and use the touch point to determine which image has been tapped? 为什么不只是在滚动视图中添加轻击手势识别器并使用触摸点来确定已点击了哪个图像? That's much easier and should work without any issues. 这很容易,应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM