Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity indicator is not shown when image is downloading - fixed #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion KDCycleBannerView/KDCycleBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ typedef void(^CompleteBlock)(void);
@protocol KDCycleBannerViewDataSource <NSObject>

@required
- (NSArray *)numberOfKDCycleBannerView:(KDCycleBannerView *)bannerView;
/**
@return Array of UIImage or NSString or NSURL objects with direct image data objects or URLs to images.
Heterogeneous array is also allowed, that is with different types of items.
*/
- (NSArray *)dataForCycleBannerView:(KDCycleBannerView *)bannerView;
- (UIViewContentMode)contentModeForImageIndex:(NSUInteger)index;

@optional
Expand All @@ -38,6 +42,19 @@ typedef void(^CompleteBlock)(void);
@property (weak, nonatomic) IBOutlet id<KDCycleBannerViewDataSource> datasource;
@property (weak, nonatomic) IBOutlet id<KDCycleBannerViewDelegate> delegate;

/**
Show activity UIActivityIndicatorView. Default `NO`.
*/
@property (assign, nonatomic) BOOL showActivityIndicatorViewWhenDownloadingImages;

/**
* set desired UIActivityIndicatorViewStyle
*
* @param style The style of the UIActivityIndicatorView
Default value `UIActivityIndicatorViewStyleWhiteLarge`.
*/
@property (assign, nonatomic) UIActivityIndicatorViewStyle activityIndicatorStyle;

@property (assign, nonatomic, getter = isContinuous) BOOL continuous; // if YES, then bannerview will show like a carousel, default is NO
@property (assign, nonatomic) NSUInteger autoPlayTimeInterval; // if autoPlayTimeInterval more than 0, the bannerView will autoplay with autoPlayTimeInterval value space, default is 0

Expand Down
45 changes: 15 additions & 30 deletions KDCycleBannerView/KDCycleBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,27 @@ - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_scrollViewBounces = YES;
[self init_common];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
_scrollViewBounces = YES;
[self init_common];
}
return self;
}

- (void)awakeFromNib {
[super awakeFromNib];
- (void)init_common {
_scrollViewBounces = YES;
_activityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
}

// MARK: - Override

- (void)layoutSubviews {
[super layoutSubviews];

NSArray *subViews = self.subviews;
[subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

[self initialize];

Expand All @@ -79,6 +78,7 @@ - (void)initialize {
}

- (void)initializeScrollView {
[_scrollView removeFromSuperview];
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
Expand All @@ -90,6 +90,7 @@ - (void)initializeScrollView {
}

- (void)initializePageControl {
[_pageControl removeFromSuperview];
CGRect pageControlFrame = CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), 30);
_pageControl = [[UIPageControl alloc] initWithFrame:pageControlFrame];
_pageControl.center = CGPointMake(CGRectGetWidth(_scrollView.frame)*0.5, CGRectGetHeight(_scrollView.frame) - 12.);
Expand All @@ -99,10 +100,9 @@ - (void)initializePageControl {

- (void)loadData {
NSAssert(_datasource != nil, @"datasource must not nil");
_datasourceImages = [_datasource numberOfKDCycleBannerView:self];
_datasourceImages = [_datasource dataForCycleBannerView:self];

if (_datasourceImages.count == 0) {
//显示默认页,无数据页面
if ([self.datasource respondsToSelector:@selector(placeHolderImageOfZeroBannerView)]) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFill;
Expand Down Expand Up @@ -137,20 +137,16 @@ - (void)loadData {
id imageSource = [_datasourceImages objectAtIndex:i];
if ([imageSource isKindOfClass:[UIImage class]]) {
imageView.image = imageSource;
}else if ([imageSource isKindOfClass:[NSString class]] || [imageSource isKindOfClass:[NSURL class]]) {
UIActivityIndicatorView *activityIndicatorView = [UIActivityIndicatorView new];
activityIndicatorView.center = CGPointMake(CGRectGetWidth(_scrollView.frame) * 0.5, CGRectGetHeight(_scrollView.frame) * 0.5);
activityIndicatorView.tag = 100;
activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[activityIndicatorView startAnimating];
[imageView addSubview:activityIndicatorView];
[imageView addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:kContentImageViewObservationContext];
} else if ([imageSource isKindOfClass:[NSString class]] || [imageSource isKindOfClass:[NSURL class]]) {
[imageView setShowActivityIndicatorView:self.showActivityIndicatorViewWhenDownloadingImages];
[imageView setIndicatorStyle:self.activityIndicatorStyle];

if ([self.datasource respondsToSelector:@selector(placeHolderImageOfBannerView:atIndex:)]) {
UIImage *placeHolderImage = [self.datasource placeHolderImageOfBannerView:self atIndex:i];
NSAssert(placeHolderImage != nil, @"placeHolderImage must not be nil");
[imageView sd_setImageWithURL:[imageSource isKindOfClass:[NSString class]] ? [NSURL URLWithString:imageSource] : imageSource placeholderImage:placeHolderImage];
}else {

} else {
[imageView sd_setImageWithURL:[imageSource isKindOfClass:[NSString class]] ? [NSURL URLWithString:imageSource] : imageSource];
}

Expand Down Expand Up @@ -223,17 +219,6 @@ - (void)autoSwitchBannerView {
[self performSelector:_cmd withObject:nil afterDelay:self.autoPlayTimeInterval];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == kContentImageViewObservationContext) {
UIImageView *imageView = (UIImageView *)object;
UIActivityIndicatorView *activityIndicatorView = (UIActivityIndicatorView *)[imageView viewWithTag:100];
[activityIndicatorView removeFromSuperview];
[imageView removeObserver:self forKeyPath:@"image"];
}
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
Expand Down
Loading