我试图在水平ScrollView上显示我的广告图像,就像App Store顶部的滚动视图一样。图像的大小为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
答案 0 :(得分: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);
我相信这应该解决这个特殊问题。但是,如果要在图片中使用动态大小,则可以使用float变量来跟踪scrollView的宽度,并在向其添加新子视图时相应地增加。