JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "UIImageView+Circle.h"
#import <objc/runtime.h>
 
static const void *UIImageViewCircleImages = @"UIImageViewCircleImages";
static const void *UIImageViewCircleStyle = @"UIImageViewCircleStyle";
static const void *UIImageViewCircleTimer = @"UIImageViewCircleTimer";
static const void *UIImageViewCircleIndex = @"UIImageViewCircleIndex";
 
@implementation UIImageView (Circle)
 
- (void)loadGifImageWith:(NSArray <NSString *> *)images TimeInterval:(NSTimeInterval)interval Style:(LCIMGCirclePlayStyle)style {
    self.images = images;
    self.style = style;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timeFire:) userInfo:nil repeats:YES];
    [self setImage:LC_IMAGENAMED(images[0])];
}
 
- (void)timeFire:(NSTimer *)timer {
    
    if (self.style == LCIMGCirclePlayStyleCircle) {
        self.index+=1;
        if (self.index==self.images.count) {
            self.index = 0;
        }
    }else if (self.style == LCIMGCirclePlayStyleOnce){
        self.index+=1;
        if (self.index==self.images.count) {
            [self releaseImgs];
        }
    }else{
       if (self.index==self.images.count) {
           self.index-=1;
        }
        if (self.index==0) {
            self.index+=1;
        }
    }
    [self setImage:LC_IMAGENAMED(self.images[self.index])];
}
 
-(void)releaseImgs{
    [self.timer invalidate];
}
 
- (NSArray<NSString *> *)images {
    return objc_getAssociatedObject(self, UIImageViewCircleImages);
}
 
- (void)setImages:(NSArray<NSString *> *)images {
    objc_setAssociatedObject(self, UIImageViewCircleImages, images, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (LCIMGCirclePlayStyle)style {
    return [objc_getAssociatedObject(self, UIImageViewCircleStyle) integerValue];
}
 
- (void)setStyle:(LCIMGCirclePlayStyle)style {
    objc_setAssociatedObject(self, UIImageViewCircleStyle, @(style), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (NSTimer *)timer {
    return objc_getAssociatedObject(self, UIImageViewCircleTimer);
}
 
- (void)setTimer:(NSTimer *)timer {
    objc_setAssociatedObject(self, UIImageViewCircleTimer, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (NSUInteger )index {
    return [objc_getAssociatedObject(self, UIImageViewCircleIndex) integerValue];
}
 
- (void)setIndex:(NSUInteger)index {
    objc_setAssociatedObject(self, UIImageViewCircleIndex, @(index), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
@end