|
| 1 | +// |
| 2 | +// UIImageView+CircularProgressView.m |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Nik Kov on 13.09.16. |
| 6 | +// Copyright © 2016 Nik Kov. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "UIImageView+CircularProgressView.h" |
| 10 | + |
| 11 | +#define TAG_PROGRESS_VIEW 777333777 |
| 12 | + |
| 13 | +@implementation UIImageView (CircularProgressView) |
| 14 | + |
| 15 | +id <ProgressViewDataSource> dataSource; |
| 16 | + |
| 17 | +#pragma mark - Custom ProgressView |
| 18 | +- (void)addCustomProgressView:(UIProgressView *)customProgressView |
| 19 | +{ |
| 20 | + UIProgressView *existingProgressView = (UIProgressView *)[self viewWithTag:TAG_PROGRESS_VIEW]; |
| 21 | + |
| 22 | + if ( ! existingProgressView) |
| 23 | + { |
| 24 | + [self setupFrameAndAttributesForProgressView:customProgressView]; |
| 25 | + [self addSubview:customProgressView]; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#pragma mark - Linear ProgressView |
| 30 | +- (void)addLinearProgressView |
| 31 | +{ |
| 32 | + UIProgressView *existingProgressView = (UIProgressView *)[self viewWithTag:TAG_PROGRESS_VIEW]; |
| 33 | + |
| 34 | + if ( ! existingProgressView) |
| 35 | + { |
| 36 | + UIProgressView *linearProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; |
| 37 | + [self setupFrameAndAttributesForProgressView:linearProgressView]; |
| 38 | + |
| 39 | + if ( dataSource ) |
| 40 | + { |
| 41 | + if ( [dataSource respondsToSelector:@selector(setupLinearProgressViewSettings)] ) |
| 42 | + { |
| 43 | + LinearProgressViewSettings *settings = [dataSource setupLinearProgressViewSettings]; |
| 44 | + if ( ! settings ) |
| 45 | + NSLog(@"Please, be sure, you are returning something in <setupLinearProgressViewSettings> method in your Data Source."); |
| 46 | + else |
| 47 | + { |
| 48 | + linearProgressView.progress = settings.progress; |
| 49 | + linearProgressView.progressTintColor = settings.progressTintColor; |
| 50 | + linearProgressView.trackTintColor = settings.trackTintColor; |
| 51 | + linearProgressView.progressImage = settings.progressImage; |
| 52 | + linearProgressView.trackImage = settings.trackImage; |
| 53 | + } |
| 54 | + } |
| 55 | + else |
| 56 | + NSLog(@"Please, implement <setupLinearProgressViewSettings> method in your Data Source."); |
| 57 | + } |
| 58 | + |
| 59 | + [self addSubview:linearProgressView]; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#pragma mark - Circular ProgressView |
| 64 | + |
| 65 | +- (void)addCircularProgressView |
| 66 | +{ |
| 67 | + DACircularProgressView *existingProgressView = (DACircularProgressView *)[self viewWithTag:TAG_PROGRESS_VIEW]; |
| 68 | + |
| 69 | + if ( ! existingProgressView) |
| 70 | + { |
| 71 | + DACircularProgressView *circularProgressView = [[DACircularProgressView alloc] init]; |
| 72 | + [self setupFrameAndAttributesForProgressView:circularProgressView]; |
| 73 | + |
| 74 | + if ( dataSource ) |
| 75 | + { |
| 76 | + if ( ! [dataSource respondsToSelector:@selector(setupCircularProgressViewSettings)] ) |
| 77 | + NSLog(@"Please, implement <setupCircularProgressViewSettings> method in your Data Source."); |
| 78 | + else |
| 79 | + { |
| 80 | + CircularProgressViewSettings *settings = [dataSource setupCircularProgressViewSettings]; |
| 81 | + if ( ! settings ) |
| 82 | + NSLog(@"Please, be sure, you are returning something in <setupCircularProgressViewSettings> method in your Data Source."); |
| 83 | + else |
| 84 | + { |
| 85 | + circularProgressView.trackTintColor = settings.trackTintColor; |
| 86 | + circularProgressView.progressTintColor = settings.progressTintColor; |
| 87 | + circularProgressView.innerTintColor = settings.innerTintColor; |
| 88 | + circularProgressView.roundedCorners = settings.roundedCorners; |
| 89 | + circularProgressView.thicknessRatio = settings.thicknessRatio; |
| 90 | + circularProgressView.clockwiseProgress = settings.clockwiseProgress; |
| 91 | + circularProgressView.progress = settings.progress; |
| 92 | + circularProgressView.indeterminateDuration = settings.indeterminateDuration; |
| 93 | + circularProgressView.indeterminate = settings.indeterminate; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + [self addSubview:circularProgressView]; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#pragma mark - Update and delete ProgressView |
| 103 | + |
| 104 | +- (void)updateProgress:(CGFloat)progress |
| 105 | +{ |
| 106 | + UIProgressView *progressView = (UIProgressView *)[self viewWithTag:TAG_PROGRESS_VIEW]; |
| 107 | + if (progressView) |
| 108 | + progressView.progress = progress; |
| 109 | +} |
| 110 | + |
| 111 | +- (void)removeProgressView |
| 112 | +{ |
| 113 | + UIView *progressView = [self viewWithTag:TAG_PROGRESS_VIEW]; |
| 114 | + if (progressView) |
| 115 | + [progressView removeFromSuperview]; |
| 116 | +} |
| 117 | + |
| 118 | +#pragma mark - Image setting methods |
| 119 | + |
| 120 | +- (void)nkv_setImageWithURL:(NSURL *)url usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 121 | +{ |
| 122 | + [self nkv_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 123 | +} |
| 124 | + |
| 125 | +- (void)nkv_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 126 | +{ |
| 127 | + [self nkv_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 128 | +} |
| 129 | + |
| 130 | +- (void)nkv_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 131 | +{ |
| 132 | + [self nkv_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 133 | +} |
| 134 | + |
| 135 | +- (void)nkv_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 136 | +{ |
| 137 | + [self nkv_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 138 | +} |
| 139 | + |
| 140 | +- (void)nkv_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 141 | +{ |
| 142 | + [self nkv_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 143 | +} |
| 144 | + |
| 145 | +- (void)nkv_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 146 | +{ |
| 147 | + [self nkv_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock usingProgressViewType:progressViewType orCustomProgressView:progressView]; |
| 148 | +} |
| 149 | + |
| 150 | +- (void)nkv_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock usingProgressViewType:(ProgressViewType)progressViewType orCustomProgressView:(UIProgressView *)progressView |
| 151 | +{ |
| 152 | + if (progressView) |
| 153 | + [self addCustomProgressView:progressView]; |
| 154 | + else |
| 155 | + switch (progressViewType) |
| 156 | + { |
| 157 | + case CircularPV: |
| 158 | + { |
| 159 | + [self addCircularProgressView]; |
| 160 | + break; |
| 161 | + } |
| 162 | + case LinearPV: |
| 163 | + default: |
| 164 | + { |
| 165 | + [self addLinearProgressView]; |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + __weak typeof(self) weakSelf = self; |
| 171 | + [self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) { |
| 172 | + |
| 173 | + CGFloat progress = ((CGFloat)receivedSize / (CGFloat)expectedSize); |
| 174 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 175 | + [weakSelf updateProgress:progress]; |
| 176 | + }); |
| 177 | + |
| 178 | + if (progressBlock) |
| 179 | + progressBlock(receivedSize, expectedSize); |
| 180 | + } |
| 181 | + |
| 182 | + completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { |
| 183 | + [weakSelf removeProgressView]; |
| 184 | + if (completedBlock) |
| 185 | + completedBlock(image, error, cacheType, imageURL); |
| 186 | + }]; |
| 187 | +} |
| 188 | + |
| 189 | +#pragma mark - Accessors and Others |
| 190 | + |
| 191 | +- (void)setupFrameAndAttributesForProgressView:(UIView*)view; |
| 192 | +{ |
| 193 | + view.tag = TAG_PROGRESS_VIEW; |
| 194 | + view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin; |
| 195 | + |
| 196 | + float width = view.frame.size.width; |
| 197 | + float height = view.frame.size.height; |
| 198 | + float x = (self.frame.size.width / 2.0) - width / 2; |
| 199 | + float y = (self.frame.size.height / 2.0) - height / 2; |
| 200 | + view.frame = CGRectMake(x, y, width, height); |
| 201 | +} |
| 202 | + |
| 203 | +- (void)nkvSetProgressViewDataSource:(id<ProgressViewDataSource>)nkvDataSource |
| 204 | +{ |
| 205 | + dataSource = nkvDataSource; |
| 206 | +} |
| 207 | + |
| 208 | +@end |
| 209 | + |
0 commit comments