123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- //
- // DPBannerView.m
- // DacaiProject
- //
- // Created by WUFAN on 14-8-29.
- // Copyright (c) 2014年 dacai. All rights reserved.
- //
- #import "DPBannerView.h"
- @implementation DPBannerScrollView
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self addObserver:self forKeyPath:@"panGestureRecognizer.state" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
- }
- return self;
- }
- - (void)dealloc {
- [self removeObserver:self forKeyPath:@"panGestureRecognizer.state"];
- }
- - (void)setDelegate:(id<DPBannerScrollViewDelegate>)delegate {
- [super setDelegate:delegate];
-
- _bannerDelegateHas.scrollViewDidBeginTracking = [delegate respondsToSelector:@selector(scrollViewDidBeginTracking:)];
- _bannerDelegateHas.scrollViewDidEndTracking = [delegate respondsToSelector:@selector(scrollViewDidEndTracking:)];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- switch (self.panGestureRecognizer.state) {
- case UIGestureRecognizerStateEnded:
- case UIGestureRecognizerStateCancelled:
- case UIGestureRecognizerStateFailed:
- if (_bannerDelegateHas.scrollViewDidEndTracking) {
- [(id<DPBannerScrollViewDelegate>)self.delegate scrollViewDidEndTracking:self];
- }
- break;
- default:
- break;
- }
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- if (_bannerDelegateHas.scrollViewDidBeginTracking) {
- [(id<DPBannerScrollViewDelegate>)self.delegate scrollViewDidBeginTracking:self];
- }
- }
- @end
- @interface DPBannerView () <UIScrollViewDelegate> {
- @private
- NSInteger _currentIndex;
- }
- @property (nonatomic, assign) CGRect lastBounds;
- @property (nonatomic, strong, readonly) UIImageView *preView;
- @property (nonatomic, strong, readonly) UIImageView *curView;
- @property (nonatomic, strong, readonly) UIImageView *nxtView;
- @property (nonatomic, assign, readonly) NSInteger preIndex;
- @property (nonatomic, assign, readonly) NSInteger curIndex;
- @property (nonatomic, assign, readonly) NSInteger nxtIndex;
- //@property (nonatomic, strong, readonly) UIPageControl *pageControl;
- @end
- @implementation DPBannerView
- @dynamic index;
- @dynamic preIndex;
- @dynamic curIndex;
- @dynamic nxtIndex;
- - (id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- [self _initialize];
- [self addSubview:self.scrollView];
- // [self addSubview:self.pageControl];
- [self.scrollView addSubview:self.preView];
- [self.scrollView addSubview:self.curView];
- [self.scrollView addSubview:self.nxtView];
- [self addGestureRecognizer:({
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pvt_onTap)];
- tap;
- })];
-
- [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
- }
- return self;
- }
- - (void)pvt_onTap {
- if (self.delegate && [self.delegate respondsToSelector:@selector(bannerView:didSelectedAtIndex:)]) {
- [self.delegate bannerView:self didSelectedAtIndex:self.curIndex];
- }
- }
- - (void)layoutSubviews {
- self.scrollView.frame = self.bounds;
- // self.pageControl.frame = CGRectMake(0, CGRectGetMaxY(self.bounds) - 20, CGRectGetWidth(self.bounds), 20);
-
- if (!CGRectEqualToRect(self.lastBounds, self.bounds)) {
- self.lastBounds = self.bounds;
-
- // 调整图片大小
- CGRect frame = self.bounds;
- self.preView.frame = frame;
- frame.origin.x += CGRectGetWidth(frame);
- self.curView.frame = frame;
- frame.origin.x += CGRectGetWidth(frame);
- self.nxtView.frame = frame;
-
- self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(frame), CGRectGetHeight(frame));
- self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(frame), 0);
- }
- }
- #pragma mark - 无限循环
- - (void)reloadData {
- if (self.imageUrls.count) {
- if (self.preIndex < self.imageUrls.count) [self.preView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.preIndex]]];
- if (self.curIndex < self.imageUrls.count) [self.curView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.curIndex]]];
- if (self.nxtIndex < self.imageUrls.count) [self.nxtView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.nxtIndex]]];
- // self.preView.image = self.images[self.preIndex];
- // self.curView.image = self.images[self.curIndex];
- // self.nxtView.image = self.images[self.nxtIndex];
- } else {
- self.preView.image = self.curView.image = self.nxtView.image = nil;
- }
-
- [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.bounds), 0)];
- // [self.pageControl setCurrentPage:_currentIndex];
- }
- - (void)autoSwitchBannerView {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
-
- if (self.duration > 0) {
- if (self.imageUrls.count > 1) {
- [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.bounds) * 2 + 1, 0) animated:YES];
- }
- [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
- }
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- if (self.scrollView.contentOffset.x < 0) {
- _currentIndex = self.preIndex;
-
- if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
- [self.delegate bannerView:self didShowAtIndex:_currentIndex];
- }
- } else if (self.scrollView.contentOffset.x > CGRectGetWidth(self.bounds) * 2) {
- _currentIndex = self.nxtIndex;
-
- if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
- [self.delegate bannerView:self didShowAtIndex:_currentIndex];
- }
- } else {
- return;
- }
- [self reloadData];
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- NSInteger index = self.scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
- if (index == 0) {
- _currentIndex = self.preIndex;
-
- if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
- [self.delegate bannerView:self didShowAtIndex:_currentIndex];
- }
-
- } else if (index == 2) {
- _currentIndex = self.nxtIndex;
-
- if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
- [self.delegate bannerView:self didShowAtIndex:_currentIndex];
- }
- } else {
- return;
- }
-
- [self reloadData];
- }
- - (void)scrollViewDidBeginTracking:(UIScrollView *)scrollView {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
- }
- - (void)scrollViewDidEndTracking:(UIScrollView *)scrollView {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
- [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
- }
- #pragma mark - method
- - (void)startAutoSwitch{
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
- [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
- }
- - (void)stopAutoSwitch{
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
- }
- #pragma mark - getter, setter
- - (void)setDuration:(CGFloat)duration {
- _duration = duration;
-
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
- [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
- }
- - (void)setIndex:(NSInteger)index {
- _currentIndex = index;
- [self reloadData];
- }
- - (NSInteger)index {
- return _currentIndex;
- }
- - (NSInteger)preIndex {
- return _currentIndex == 0 ? (self.imageUrls.count - 1) : (_currentIndex - 1);
- }
- - (NSInteger)curIndex {
- return _currentIndex;
- }
- - (NSInteger)nxtIndex {
- return _currentIndex == (self.imageUrls.count - 1) ? 0 : (_currentIndex + 1);
- }
- - (void)setImageUrls:(NSArray *)imageUrls {
- _imageUrls = imageUrls;
-
- _currentIndex = 0;
- _scrollView.scrollEnabled = imageUrls.count > 1;
- // _pageControl.numberOfPages = imageUrls.count;
-
- [self reloadData];
- }
- #pragma mark - initialize
- - (void)_initialize {
- [self setBackgroundColor:[UIColor clearColor]];
-
- // _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
- // _pageControl.hidesForSinglePage = YES;
- // _pageControl.userInteractionEnabled = NO;
-
- // view
- _scrollView = [[DPBannerScrollView alloc] init];
- _scrollView.delegate = self;
- _scrollView.pagingEnabled = YES;
- _scrollView.scrollEnabled = NO;
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.delaysContentTouches = NO;
- _scrollView.scrollsToTop = NO;
- _scrollView.bounces = NO;
-
- _preView = [[UIImageView alloc] init];
- _curView = [[UIImageView alloc] init];
- _nxtView = [[UIImageView alloc] init];
-
-
- // loop
- _duration = 5.0f;
- }
- - (void)setDefalutContentModel:(UIViewContentMode)defalutContentModel{
- _preView.contentMode = defalutContentModel;
- _curView.contentMode = defalutContentModel;
- _nxtView.contentMode = defalutContentModel;
- }
- @end
|