11
11
12
12
#pragma mark - SPI
13
13
14
- #define kCGImageAnimationStatus_Uninitialized -1
14
+ static UIImage * SharedEmptyImage (void ) {
15
+ // This is used for placeholder on `WKInterfaceImage`
16
+ // Do not using `[UIImage new]` because WatchKit will ignore it
17
+ static dispatch_once_t onceToken;
18
+ static UIImage *image;
19
+ dispatch_once (&onceToken, ^{
20
+ UIColor *color = UIColor.clearColor ;
21
+ CGRect rect = WKInterfaceDevice.currentDevice .screenBounds ;
22
+ UIGraphicsBeginImageContext (rect.size );
23
+ CGContextRef context = UIGraphicsGetCurrentContext ();
24
+ CGContextSetFillColorWithColor (context, [color CGColor ]);
25
+ CGContextFillRect (context, rect);
26
+ image = UIGraphicsGetImageFromCurrentImageContext ();
27
+ UIGraphicsEndImageContext ();
28
+ });
29
+ return image;
30
+ }
15
31
16
32
@protocol CALayerProtocol <NSObject >
17
33
@property (nullable , strong ) id contents;
@@ -30,8 +46,9 @@ @protocol UIViewProtocol <NSObject>
30
46
31
47
@end
32
48
33
- @protocol UIImageViewProtocol <NSObject >
49
+ @protocol UIImageViewProtocol <UIViewProtocol >
34
50
51
+ @property (nullable , nonatomic , strong ) UIImage *image;
35
52
- (void )startAnimating ;
36
53
- (void )stopAnimating ;
37
54
@property (nonatomic , readonly , getter =isAnimating) BOOL animating;
@@ -43,7 +60,7 @@ @interface WKInterfaceObject ()
43
60
// This is needed for dynamic created WKInterfaceObject, like `WKInterfaceMap`
44
61
- (instancetype )_initForDynamicCreationWithInterfaceProperty : (NSString *)property ;
45
62
// This is remote UIView
46
- @property (nonatomic , strong , readonly ) id <UIViewProtocol > _interfaceView;
63
+ @property (nonatomic , strong , readonly ) id <UIImageViewProtocol > _interfaceView;
47
64
48
65
@end
49
66
@@ -80,28 +97,10 @@ - (NSDictionary *)interfaceDescriptionForDynamicCreation {
80
97
return @{
81
98
@" type" : @" image" ,
82
99
@" property" : self.interfaceProperty ,
83
- @" image" : [ self .class sharedEmptyImage ]
100
+ @" image" : SharedEmptyImage ()
84
101
};
85
102
}
86
103
87
- + (UIImage *)sharedEmptyImage {
88
- // This is used for placeholder on `WKInterfaceImage`
89
- // Do not using `[UIImage new]` because WatchKit will ignore it
90
- static dispatch_once_t onceToken;
91
- static UIImage *image;
92
- dispatch_once (&onceToken, ^{
93
- UIColor *color = UIColor.clearColor ;
94
- CGRect rect = CGRectMake (0 , 0 , 1 , 1 );
95
- UIGraphicsBeginImageContext (rect.size );
96
- CGContextRef context = UIGraphicsGetCurrentContext ();
97
- CGContextSetFillColorWithColor (context, [color CGColor ]);
98
- CGContextFillRect (context, rect);
99
- image = UIGraphicsGetImageFromCurrentImageContext ();
100
- UIGraphicsEndImageContext ();
101
- });
102
- return image;
103
- }
104
-
105
104
- (void )setImage : (UIImage *)image {
106
105
if (_image == image) {
107
106
return ;
@@ -115,6 +114,7 @@ - (void)setImage:(UIImage *)image {
115
114
self.currentLoopCount = 0 ;
116
115
117
116
[super setImage: image];
117
+ [self _interfaceView ].image = image;
118
118
if ([image.class conformsToProtocol: @protocol (SDAnimatedImage)]) {
119
119
// Create animted player
120
120
self.player = [SDAnimatedImagePlayer playerWithProvider: (id <SDAnimatedImage>)image];
@@ -148,30 +148,19 @@ - (void)setImage:(UIImage *)image {
148
148
sself.currentLoopCount = loopCount;
149
149
};
150
150
151
- // Update should animate
152
- [self updateShouldAnimate ];
153
- if (self.shouldAnimate ) {
154
- [self startAnimating ];
155
- }
151
+ // Start animating
152
+ [self startAnimating ];
156
153
157
154
[self displayLayer: self .imageViewLayer];
158
155
}
159
156
}
160
157
161
158
- (void )updateAnimation {
162
159
[self updateShouldAnimate ];
163
- if (!self.player ) {
164
- return ;
165
- }
166
- // Filter automatically animating changes
167
160
if (self.shouldAnimate && self.isAnimating ) {
168
- [self .player startPlaying ];
169
- } else if (!self.shouldAnimate && !self.isAnimating ) {
170
- if (self.resetFrameIndexWhenStopped ) {
171
- [self .player stopPlaying ];
172
- } else {
173
- [self .player pausePlaying ];
174
- }
161
+ [self startAnimating ];
162
+ } else {
163
+ [self stopAnimating ];
175
164
}
176
165
}
177
166
@@ -198,7 +187,10 @@ - (void)updateShouldAnimate
198
187
- (void )startAnimating {
199
188
self.animating = YES ;
200
189
if (self.player ) {
201
- [self .player startPlaying ];
190
+ [self updateShouldAnimate ];
191
+ if (self.shouldAnimate ) {
192
+ [self .player startPlaying ];
193
+ }
202
194
} else if (_image.images .count > 0 ) {
203
195
[super startAnimating ];
204
196
}
0 commit comments