我试图制作类似于iOS中的照片应用程序中的ScrollView,但在编写以下代码行后,我似乎仍然遇到了一些问题
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong,nonatomic) NSArray * ContentList;
@property (strong,nonatomic) NSMutableArray * pageViews;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *PageControl;
@end
@implementation ViewController
-(NSArray *)ContentList
{
if (!_ContentList) {
_ContentList = @[[UIImage imageNamed:@"Photo_1"],[UIImage imageNamed:@"Photo_2"],[UIImage imageNamed:@"Photo_3"],[UIImage imageNamed:@"Photo_4"],[UIImage imageNamed:@"Photo_5"],[UIImage imageNamed:@"Photo_7"]];
}
return _ContentList;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self ContentList];
NSUInteger numOfpages = _ContentList.count;
NSMutableArray * views = [[NSMutableArray alloc]init];
for (NSUInteger i = 0; i < numOfpages; i++) {
[views addObject:[NSNull null]];
}
self.pageViews = views;
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize =
CGSizeMake(CGRectGetWidth(self.scrollView.frame) * numOfpages, CGRectGetHeight(self.scrollView.frame));
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
_PageControl.numberOfPages = numOfpages;
_PageControl.currentPage = 0;
NSLog(@"the number of objects in the array is %lu",_ContentList.count);
NSLog(@"the number of objects in the mutable array is %lu",_pageViews.count);
[self LoadScrollViewWithPage:0];
[self LoadScrollViewWithPage:1];
}
-(void)LoadScrollViewWithPage:(NSInteger)page
{
if (page >= self.ContentList.count ||page < 0)
return;
// replace the placeholder if necessary
UIView * view = [self.pageViews objectAtIndex:page];
if ((NSNull *)view == [NSNull null])
{
UIImageView * newView = [[UIImageView alloc]initWithImage:[_ContentList objectAtIndex:page]];
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * page;
frame.origin.y = 0;
newView.frame = frame;
newView.clipsToBounds = YES;
newView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:newView];
[_pageViews replaceObjectAtIndex:page withObject:newView];
}
}
-(void)PurgePage:(NSInteger)page
{
if (page < 0 || page > _ContentList.count) {
return;
}
UIView * view = [_pageViews objectAtIndex:page];
if ((NSNull *)view != [NSNull null]) {
[view removeFromSuperview];
[_pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
NSUInteger Currentpage = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.PageControl.currentPage = Currentpage;
NSInteger PagebeforeCurrentPage = Currentpage - 1;
NSInteger PageAfterCurrentPage = Currentpage + 1;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
// a possible optimization would be to unload the views+controllers which are no longer visible
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
for (NSInteger i = 0; i < PagebeforeCurrentPage; i++) {
[self PurgePage: i];
}
[self LoadScrollViewWithPage:PagebeforeCurrentPage];
[self LoadScrollViewWithPage:Currentpage];
[self LoadScrollViewWithPage:PageAfterCurrentPage];
for (NSInteger p = PageAfterCurrentPage; p < _pageViews.count; p++)
{
[self PurgePage:p];
}
}
请帮助