| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // PageScrollView.m
- // CommonLibrary
- //
- // Created by Alexi Chen on 3/5/13.
- // Copyright (c) 2013 AlexiChen. All rights reserved.
- //
- #import "PageScrollView.h"
- #import "UIView+Layout.h"
- #import "IOSDeviceConfig.h"
- #import "NSString+Common.h"
- #import "UIView+CustomAutoLayout.h"
- @interface PageScrollView ()
- @end
- @implementation PageScrollView
- - (void)addScrollView
- {
- _scrollView = [[UIScrollView alloc] init];
- _scrollView.pagingEnabled = YES;
- _scrollView.delegate = self;
- _scrollView.bounces = NO;
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.scrollsToTop = NO;
- _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
- [self addSubview:_scrollView];
-
- _pageControl = [[UIPageControl alloc] init];
- // _pageControl.numberOfPages = _images.count;
- // _pageControl.currentPage = 0;
- // [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
- [self addSubview:_pageControl];
- _pageControl.hidden = YES;
- }
- - (void)setPageIndex:(NSInteger)pageIndex
- {
- _pageIndex = pageIndex;
- _pageControl.currentPage = pageIndex;
- }
- - (void)changeToPage:(NSInteger)page manual:(BOOL)isManual
- {
- [self loadPage:page - 1 feedBack:NO];
- [self loadPage:page feedBack:isManual];
- [self loadPage:page + 1 feedBack:NO];
-
- CGRect frame = _scrollView.frame;
- frame.origin.x = frame.size.width * page;
- frame.origin.y = 0;
- [_scrollView scrollRectToVisible:frame animated:NO];
-
- _pageControlUsed = YES;
- }
- - (void)changePage:(id)sender
- {
- NSInteger page = _pageControl.currentPage;
- [self changeToPage:page manual:NO];
- }
- - (void)addOwnViews
- {
- [self addScrollView];
- }
- - (void)roratePages
- {
- for (NSInteger page = 0; page < _pages.count; page++)
- {
- UIView *view = [_pages objectAtIndex:page];
- if (view.superview != nil)
- {
- CGRect rect = _scrollView.bounds;
- rect.origin.x = rect.size.width * page;
- [view setFrameAndLayout:rect];
- }
- }
- }
- - (void)setFrameAndLayout:(CGRect)rect withPages:(NSArray *)pages
- {
- if (!pages) {
- return;
- }
- if (_pages != pages)
- {
- self.pages = pages;
- }
-
-
- [self setFrameAndLayout:rect];
- // 设置scrollView相关的内容
- _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * _pages.count, _scrollView.frame.size.height);
-
- _pageControl.numberOfPages = self.pages.count;
- _pageControl.currentPage = _pageIndex;
- [self roratePages];
-
-
- [self loadPage:_pageIndex - 1 feedBack:NO];
- [self loadPage:_pageIndex feedBack:NO];
- [self loadPage:_pageIndex + 1 feedBack:NO];
-
- // [self loadPage:_pageIndex feedBack:NO];
- CGPoint p = _scrollView.contentOffset;
- p.x = _pageIndex * _scrollView.frame.size.width;
- _scrollView.contentOffset = p;
-
- if (_pageIndex >= 0 && _pageIndex < _pages.count)
- {
- if (_pageScrollDelegate && [_pageScrollDelegate respondsToSelector:@selector(onPageScrollView:scrollToPage:)])
- {
- [_pageScrollDelegate onPageScrollView:self scrollToPage:_pageIndex];
- }
- }
-
- }
- #define kPageIndicatorHeight 10
- - (CGRect)pageIndicatorRect
- {
- CGRect rect = self.bounds;
-
- rect.origin.y += rect.size.height - kPageIndicatorHeight;
- rect.size.height = kPageIndicatorHeight;
- rect = CGRectInset(rect, 9, (kPageIndicatorHeight - 5)/2);
- CGFloat pw = rect.size.width / _pages.count;
-
- CGFloat pageWidth = _scrollView.frame.size.width;
- int page = floor(((_scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1);
-
- rect.origin.x += page * pw;
- rect.size.width = pw;
- return rect;
- }
- - (void)relayoutFrameOfSubViews
- {
- _scrollView.frame = self.bounds;
-
- [_pageControl layoutParentHorizontalCenter];
-
- [_pageControl alignParentBottomWithMargin:50];
- }
- - (void)loadPage:(NSInteger)page feedBack:(BOOL)need
- {
- if (page < 0) {
- return;
- }
-
- if (page >= _pages.count) {
- return;
- }
-
- if (_pages.count == 0) {
- return;
- }
-
- UIView *view = [_pages objectAtIndex:page];
-
- if (view.superview == nil)
- {
- CGRect rect = _scrollView.bounds;
- rect.origin.x = rect.size.width * page;
- [view setFrameAndLayout:rect];
- [_scrollView addSubview:view];
- }
- else
- {
- CGRect rect = _scrollView.bounds;
- rect.origin.x = rect.size.width * page;
- [view setFrameAndLayout:rect];
- }
-
- if (need)
- {
- if (_pageIndex >= 0 && _pageIndex < _pages.count)
- {
- if (_pageScrollDelegate && [_pageScrollDelegate respondsToSelector:@selector(onPageScrollView:scrollToPage:)])
- {
- [_pageScrollDelegate onPageScrollView:self scrollToPage:_pageIndex];
- }
- }
- }
-
- }
- - (void)scrollTo:(NSInteger)pageIndex
- {
- CGFloat pageWidth = _scrollView.frame.size.width;
- CGPoint p = _scrollView.contentOffset;
- p.x = pageIndex * pageWidth;
- _scrollView.contentOffset = p;
-
- // DebugLog(@"%@", NSStringFromCGPoint(p));
- self.pageIndex = pageIndex;
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- if (_pageControlUsed)
- {
- return;
- }
- // DebugLog(@"scrollViewDidScroll");
- CGFloat pageWidth = _scrollView.frame.size.width;
- NSInteger pi = floor(((_scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1);
-
- if (_pageIndex == pi) {
- return;
- }
- else
- {
- self.pageIndex = pi;
- [self loadPage:_pageIndex - 1 feedBack:NO];
- [self loadPage:_pageIndex feedBack:YES];
- [self loadPage:_pageIndex + 1 feedBack:NO];
- }
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- _pageControlUsed = NO;
- }
- - (void)setPageFrame:(NSInteger)page
- {
- if (page < 0) {
- return;
- }
-
- if (page >= _pages.count) {
- return;
- }
-
- if (_pages.count == 0) {
- return;
- }
-
- UIView *view = [_pages objectAtIndex:page];
-
- if (view.superview == nil)
- {
- CGRect rect = _scrollView.bounds;
- rect.origin.x = rect.size.width * page;
- [view setFrameAndLayout:rect];
- [_scrollView addSubview:view];
- }
- }
- - (NSInteger)pageCount
- {
- return [_pages count];
- }
- @end
|