无法使用 IOS 的 ScrollView 水平滚动
Posted
技术标签:
【中文标题】无法使用 IOS 的 ScrollView 水平滚动【英文标题】:Cannot scroll horizontally with ScrollView for IOS 【发布时间】:2014-03-12 01:21:36 【问题描述】:我试图在水平滚动视图上显示我的广告图像,就像应用商店顶部的滚动视图一样。图片尺寸为320*180。
我的问题是:如果我将滚动视图的框架设置为 320*568,它工作得很好,但如果我将它设置为 320*180(应该是),则 ScrollView 不再滚动。
这是我的代码:
#import "AdsScrollViewController.h"
@interface AdsScrollViewController ()
@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, strong) NSMutableArray *pageViews;
- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;
@end
@implementation AdsScrollViewController
@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize pageImages = _pageImages;
@synthesize pageViews = _pageViews;
- (void)loadPage:(NSInteger)page
if (page < 0 || page >= self.pageImages.count)
// If it's outside the range of what you have to display, then do nothing
return;
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null])
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
- (void)purgePage:(NSInteger)page
if (page < 0 || page >= self.pageImages.count)
// If it's outside the range of what you have to display, then do nothing
return;
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null])
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
- (void)loadVisiblePages
// First, determine which page is currently visible
NSLog(@"load visible pages");
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++)
[self purgePage:i];
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++)
[self loadPage:i];
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++)
[self purgePage:i];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
self.pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ad1.jpg"],
[UIImage imageNamed:@"ad2.jpg"],
[UIImage imageNamed:@"ad3.jpg"],
[UIImage imageNamed:@"ad1.jpg"],
[UIImage imageNamed:@"ad2.jpg"],
nil];
NSInteger pageCount = 5;//self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i)
[self.pageViews addObject:[NSNull null]];
self.scrollView.delegate = self;
-(void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
// 4
[_scrollView setScrollEnabled:YES];
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * 5, 568);// pagesScrollViewSize.height);
NSLog(@"view will appear");
NSLog(@"%f, %f",self.scrollView.frame.size.height,self.scrollView.frame.size.width);
// 5
[self loadVisiblePages];
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
NSLog(@"scrollViewDidScroll");
// Load the pages which are now on screen
[self loadVisiblePages];
@end
【问题讨论】:
您应该尝试的第一件事是删除所有页面加载和清除代码,只需将尺寸设置为 320x180 并手动添加一些视图。检查它是否滚动。然后添加自动加载(但不清除)并重试,等等。您会将问题代码缩小到特定功能,然后您可以将问题更改为“这个特定功能导致滚动视图停止滚动,我不这样做不知道为什么”。 我遇到过几次这个问题,总是通过手动将 scrollView.contentSize 设置为正在显示的内容的 contentSize 来解决。请检查这是否有效。 【参考方案1】:假设,如您所说,您要显示的所有图像都是 320x180 或相同的纵横比,您可以在 loadPage 中更改以下代码:
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
到以下块:
CGRect frame = CGRectMake(320*page, 0 , 320, 180);
self.scrollView.contentSize = CGSizeMake(320*(page+1),180);
我相信这应该可以解决这个特殊问题。但是,如果您想在图片中具有动态大小,则可以使用一个浮点变量来跟踪滚动视图的宽度并在您向其添加新子视图时相应地递增。
【讨论】:
感谢您的帮助。它仍然无法正常工作。由于我在 viewWillAppear 函数中设置了 scrollView.contentSize,您认为我应该先删除它们,然后按照您说的更改代码吗?以上是关于无法使用 IOS 的 ScrollView 水平滚动的主要内容,如果未能解决你的问题,请参考以下文章
水平 ScrollView 在 SwiftUI 中在更改时垂直反弹
ListView 不在 ScrollView 和 StackLayout 内呈现,在 ios 中具有水平方向 - nativescript-vue
Android:ScrollView包含两个嵌套的ConstraintLayout,无法水平填满屏幕