Ios 将UIScrollView按钮添加为子视图时不滚动UIScrollView

Ios 将UIScrollView按钮添加为子视图时不滚动UIScrollView,ios,uiscrollview,Ios,Uiscrollview,我正在尝试构建一个简单的UIScrollView,通过分页在3个图像之间水平滚动。 棘手的是,我希望每个图像都可以点击,并捕捉点击事件 我的技术是创建3个UIButton,每个UIButton由UIImage组成。给每个按钮一个标签并设置一个操作 问题:我可以捕获单击事件-但它不可滚动 这是我的密码: - (void) viewDidAppear:(BOOL)animated { _imageArray = [[NSArray alloc] initWithObjects:@"cont

我正在尝试构建一个简单的UIScrollView,通过分页在3个图像之间水平滚动。 棘手的是,我希望每个图像都可以点击,并捕捉点击事件

我的技术是创建3个UIButton,每个UIButton由UIImage组成。给每个按钮一个标签并设置一个操作

问题:我可以捕获单击事件-但它不可滚动

这是我的密码:

- (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;
}
-(void)视图显示:(BOOL)动画{
_imageArray=[[NSArray alloc]initWithObjects:@“content_01.png”,“content_02.png”,“content_03.png”,nil];
对于(int i=0;i<[u imageArray count];i++){
//我们将在scrollView的每个“页面”中创建一个imageView对象。
CGRect帧;
frame.origin.x=\u contentScrollView.frame.size.width*i;
frame.origin.y=0;
frame.size=\u contentScrollView.frame.size;
//
//获取要使用的图像,无论您想要什么
UIImage*image=[UIImage ImageName:[\u imageArray objectAtIndex:i]];
UIButton*button=[[UIButton alloc]initWithFrame:frame];
//设置要为其显示图像的按钮状态
[按钮设置图像:图像状态:uicontrol状态正常];
[按钮设置图像:状态图像:uicontrol状态高亮显示];
//创建触摸事件目标,我调用'productImagePressed'方法
[按钮addTarget:自操作:@选择器(按下图像:)
forControlEvents:UIControlEventTouchUpInside];
//我将标签设置为图像#,我正在查看它们的数组
button.tag=i;
[\u contentScrollView添加子视图:按钮];
}
//根据imageView对象的总宽度设置scrollview的内容大小。
_contentScrollView.contentSize=CGSizeMake(_contentScrollView.frame.size.width*[_ImageArrayCount],_contentScrollView.frame.size.height);
_contentScrollView.backgroundColor=[ENGAppDelegate backgroundColor];
_contentScrollView.delegate=self;
}

由于
UIButton
UIControl
子类,它“吞噬”了滚动视图的触动:

[UIScrollView touchesShouldCancelInContentView:
如果视图不是UIControl对象,则默认返回值为YES;否则,它返回NO

(发件人:)


您可以通过子类化
UIScrollView
并覆盖
ToucheShouldCancelIncontentView:
(和/或
ToucheShouldBegin:withEvent:inContentView:
)来影响这一点。然而,对于您的用例,我首先不会使用按钮。为什么不在scrollview中添加一个点击手势识别器,并使用触摸点来确定点击了哪个图像?这会更容易,而且应该不会出现任何问题。

您是否仔细检查了代码,以确保_contentScrollView.contentSize是您所期望的?我认为此时未正确计算_contentScrollView.frame.size,因此不会得到一个好的值。我已检查并正确计算了它!让我这样说:如果我启动没有边框的按钮(因此没有显示图片),那么我看到它滚动!是否有可能,如果我将按钮框设置为与ScrollView相同的精确大小,那么它会完全阻止滚动,因此我无法获得滚动事件??这对我来说似乎很奇怪。滚动视图不应该关心其子对象的帧。计算结束时,您的_contentScrollView.frame是什么?你有没有碰巧把它做得和内容一样大,而不仅仅是屏幕大小?