为啥这个基于 UIScrollView 的轮播不响应水平滑动?

Posted

技术标签:

【中文标题】为啥这个基于 UIScrollView 的轮播不响应水平滑动?【英文标题】:Why is this UIScrollView-based carousel not responding to horizontal swipes?为什么这个基于 UIScrollView 的轮播不响应水平滑动? 【发布时间】:2013-09-24 20:05:05 【问题描述】:

我有,稍微简化一下:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
    200 /*self.view.frame.size.width*/,
    (self.view.frame.size.height - 20) / 3)];

UIImage *cardImage = [UIImage imageNamed:@"note1"];
CGSize cardSize = [cardImage size];
CJSHCardView *cardView = [[CJSHCardView alloc] initWithImage:cardImage];
[cardView rotate];
[cardView setContentMode:UIViewContentModeScaleAspectFit];
[cardView setFrame:CGRectMake(0, 20, cardSize.width * scale, cardSize.height * scale)];
[cardView setDescription:@"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"];
[cardView setOnWeb:[NSURL URLWithString:@"https://www.google.com"]];
[scrollView addSubview:cardView];

for(int i = 0; i < 10; ++i)

    UIImage *noteImage2 = [UIImage imageNamed:@"note2"];
    CGSize noteSize2 = [noteImage2 size];
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:noteImage2];
    CGRect bounds2 = imageView2.bounds;
    CJSHCardView *noteView2 = [[CJSHCardView alloc] initWithImage:noteImage2];
    [noteView2 rotate];
    [noteView2 setContentMode:UIViewContentModeScaleAspectFit];
    [noteView2 setFrame:CGRectMake(100 + 100 * i, 20, noteSize2.width * scale, noteSize2.height * scale)];
    [scrollView addSubview:noteView2];

[containerView addSubview:scrollView];

在 200 像素宽或注释掉的宽度下,它会显示一定数量的图像,最右边的图像会被截断。但是, UIScrollView 不滚动。我试过左右(单)手指滑动,图像没有移动一个像素。

这里有什么问题?我是否需要进行额外的配置才能获得类似轮播的滚动?有没有更好的类来实现在单个维度上滚动的轮播?

谢谢,

【问题讨论】:

【参考方案1】:

为了在 iOS 上实现轮播,我发现这个项目的实现非常灵活且易于使用:

https://github.com/nicklockwood/iCarousel

其中包含一些很好的示例,可以让您快速了解使用情况。

希望这会有所帮助!

【讨论】:

谢谢;调查它。 我在看它,从积极的角度来看,它似乎是工业实力。 “iOS Basic”演示至少与我自己编写代码所期望的一样多;我不是在寻找一个可以满足任何用例/要求的终极轮播,只是一个基本的非喷气式轮播,它可能使用 UIScrollView 来允许在一个维度上滑动并具有基本的滚动行为。 或者,也许您可​​以扩展您的答案以在其父视图的不同位置包含一组图像,并适当使用轮播以允许在父视图中滑动。

以上是关于为啥这个基于 UIScrollView 的轮播不响应水平滑动?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的轮播图片不是一张一张的在图框轮播,而是四张图片排成一列?有图

为啥我的轮播顶部的按钮不起作用?

bootstrap轮播 为啥我的轮播没有用

为啥我的轮播滑块中的图像没有响应?

为啥我在引导程序的轮播组件中看不到上一个和下一个箭头?

bootstrap 轮播craousel 采坑之(修改默认鼠标浮动轮播不停止)