首页 > 代码库 > scrollView广告轮播
scrollView广告轮播
<span style="color:#33ccff;">// // CarouselSrollView.m // CarouselScrollerView // // Created by xiaoyao on 15/1/12. // Copyright (c) 2015年 lijien. All rights reserved. // #import "CarouselSrollView.h" #import "UIImageView+WebCache.h" #define kScrollTimerInterVal 5.0 @interface CarouselSrollView ()<UIScrollViewDelegate> { UIScrollView *_carouselScrollew; // 滚动视图 UIPageControl *_pageControl; // 控制分页 NSArray *_imagesArray; // 存放所有图片对象 NSMutableArray *_currentPageArray; // 存放当前滚动三页图片对象 NSUInteger _totalPage; // 总页数 NSUInteger _currentPage; // 当前页码 CGRect _scrollViewFrame; } @property (nonatomic, strong) NSTimer *timer; @end @implementation CarouselSrollView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _imagesArray = [[NSArray alloc] init]; _currentPageArray = [[NSMutableArray alloc] init]; _scrollViewFrame = frame; _carouselScrollew = [[UIScrollView alloc] initWithFrame:_scrollViewFrame]; _carouselScrollew.backgroundColor = [UIColor clearColor]; _carouselScrollew.showsHorizontalScrollIndicator = NO; _carouselScrollew.showsVerticalScrollIndicator = NO; _carouselScrollew.pagingEnabled = YES; _carouselScrollew.delegate = self; NSUInteger count = _imagesArray.count >= 3 ? 3 : _imagesArray.count; _carouselScrollew.contentSize = CGSizeMake(count * _scrollViewFrame.size.width, _scrollViewFrame.size.height); [self addSubview:_carouselScrollew]; CGFloat px = (self.frame.size.width - 28) / 2; CGFloat py = self.frame.size.height - 28; CGFloat pw = 28; CGFloat ph = 28; _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(px, py, pw, ph)]; _pageControl.pageIndicatorTintColor = [UIColor whiteColor]; _pageControl.currentPageIndicatorTintColor = [UIColor grayColor]; _currentPage = 1; _totalPage = _imagesArray.count; _pageControl.currentPage = _currentPage; _pageControl.numberOfPages = _totalPage; [self addSubview:_pageControl]; [self openTimer]; } return self; } - (void)startTimer { [self.timer setFireDate:[NSDate distantPast]]; } - (void)stopTimer { [self.timer setFireDate:[NSDate distantFuture]]; } - (void)openTimer { // 每次启动之前将之前的定时器销毁 [self.timer invalidate]; self.timer = nil; self.timer = [NSTimer scheduledTimerWithTimeInterval:kScrollTimerInterVal target:self selector:@selector(autoScrollToNext) userInfo:nil repeats:YES]; } #pragma mark - 定时器调用滚动到下一页 - (void)autoScrollToNext { _currentPage = [self vaildPage:_currentPage + 1]; return; } /** * @brief 得到当前有效的页码 * @author lijien * @date 2014-01-13 */ - (NSUInteger)vaildPage:(NSInteger)valuePage { if (valuePage == 0) { valuePage = _totalPage; } else { valuePage = _totalPage + 1; valuePage = 1; } return valuePage; } - (void)setImagePaths:(NSArray *)imagePathArray { if (_imagesArray == nil) { _imagesArray = [[NSArray alloc] initWithArray:imagePathArray]; } else { _imagesArray = [NSArray arrayWithArray:imagePathArray]; } _currentPage = 1; _totalPage = _imagesArray.count; _pageControl.currentPage = _currentPage; _pageControl.numberOfPages = _totalPage; _pageControl.hidesForSinglePage = YES; // 只有一项的时候隐藏,默认是NO NSUInteger count = _imagesArray.count >= 3 ? 3 : _imagesArray.count; _carouselScrollew.contentSize = CGSizeMake(count * _scrollViewFrame.size.width, _scrollViewFrame.size.height); [self openTimer]; return; } /** * @brief 刷新滚动视图 * @author lijien * @date 2014-01-13 */ - (void)refreshScrolView { [self disPlayImageWithCurrentImage:_currentPage]; for (NSUInteger index = 0; index < _currentPageArray.count; index++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:_scrollViewFrame]; imageView.frame = CGRectOffset(_scrollViewFrame, _scrollViewFrame.size.width * index, 0); NSString *imgUrl = [_currentPageArray objectAtIndex:index]; if (imgUrl != nil) { [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"aaa"]]; } imageView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapClicked:)]; [imageView addGestureRecognizer:tap]; [_carouselScrollew addSubview:imageView]; } [_carouselScrollew setContentOffset:CGPointMake(_scrollViewFrame.size.width, 0)]; if ([self.delegate respondsToSelector:@selector(carScrollView:didScrollImageToIndex:)]) { [self.delegate carScrollView:_carouselScrollew didScrollImageToIndex:_currentPage]; } } - (void)imageViewTapClicked:(UIGestureRecognizer *)tap { } /** * @brief 根据当前页得到存放的当前的三张图片 * @author lijien * @date 2014-01-13 */ - (NSArray *)disPlayImageWithCurrentImage:(NSUInteger)currentPage { NSUInteger previousPage = [self vaildPage:currentPage - 1]; NSUInteger nextPage = [self vaildPage:currentPage + 1]; if (_currentPageArray.count != 0) { [_currentPageArray removeAllObjects]; } if (previousPage - 1 < _imagesArray.count) { [_currentPageArray addObject:[_imagesArray objectAtIndex:previousPage - 1]]; } if (currentPage - 1 < _imagesArray.count) { [_currentPageArray addObject:[_imagesArray objectAtIndex:currentPage -1]]; } if (nextPage - 1 < _imagesArray.count) { [_currentPageArray addObject:[_imagesArray objectAtIndex:nextPage - 1]]; } return _currentPageArray; } #pragma mark - scrollViewDlelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.x >= 2 * _scrollViewFrame.size.width) { _currentPage = [self vaildPage:_currentPage + 1]; [self refreshScrolView]; if ([self.delegate respondsToSelector:@selector(carScrollView:didScrollImageToIndex:)]) { [self.delegate carScrollView:_carouselScrollew didScrollImageToIndex:_currentPage]; } } if (scrollView.contentOffset.x <= 0) { _currentPage = [self vaildPage:_currentPage - 1]; [self refreshScrolView]; if ([self.delegate respondsToSelector:@selector(carScrollView:didScrollImageToIndex:)]) { [self.delegate carScrollView:_carouselScrollew didScrollImageToIndex:_currentPage]; } } return; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [scrollView setContentOffset:CGPointMake(_scrollViewFrame.size.width, 0)]; // 再次启用定时器 if (self.timer == nil) { [self openTimer]; } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 在拖拽之前先销毁之前的定时器对象 if(self.timer) { [_timer invalidate]; _timer = nil; } return; } @end </span>
scrollView广告轮播
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。