UIImageViews在分页UIScrollView中相互重叠

时间:2015-05-12 22:07:02

标签: ios objective-c iphone uiscrollview xcode6

我试图制作类似于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];
}
  }
  1. 即使我将内容模式设置为UIViewContentModeAspectFit,滚动视图中的前2个视图也会比它们更大。
  2. 在完全访问该页面之前,您无法看到要滚动到的下一页,即只有当您在该页面上时才会显示该视图
  3. 第三个视图重叠,它位于第二个视图的顶部
  4. 滚动浏览第3个视图后,第1个和第2个视图缩小回正常大小
  5. 你仍然可以在最后一个视图后滚动,如果你滚动之后你会得到一个NSRangeException,说“ - [__ NSArrayM objectAtIndex:]:索引6超出界限[0 .. 5]”
  6. 请帮助

0 个答案:

没有答案