-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRNFLAnimatedImage.m
90 lines (70 loc) · 2.36 KB
/
RNFLAnimatedImage.m
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
82
83
84
85
86
87
88
89
//
// RNFLAnimatedImage.m
// react_native_flanimatedimage
//
// Created by Jason Brown on 11/23/15.
// Copyright © 2015 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "FLAnimatedImage/FLAnimatedImage.h"
#import "RCTBridgeModule.h"
#import "RCTEventDispatcher.h"
#import "UIView+React.h"
#import "RCTLog.h"
#import "RNFLAnimatedImage.h"
@implementation RNFLAnimatedImage : UIView {
RCTEventDispatcher *_eventDispatcher;
FLAnimatedImage *_image;
FLAnimatedImageView *_imageView;
}
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super init])) {
_eventDispatcher = eventDispatcher;
_imageView = [[FLAnimatedImageView alloc] init];
[_imageView addObserver:self forKeyPath:@"currentFrameIndex" options:0 context:nil];
}
return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if (object == _imageView) {
if ([keyPath isEqualToString:@"currentFrameIndex"]) {
[_eventDispatcher sendInputEventWithName:@"onFrameChange" body:@{
@"currentFrameIndex":[NSNumber numberWithUnsignedInteger:[object currentFrameIndex]],
@"frameCount": [NSNumber numberWithUnsignedInteger:[_image frameCount]],
@"target": self.reactTag
}];
}
}
}
- (void)removeFromSuperview
{
[_imageView removeObserver:self forKeyPath:@"currentFrameIndex"];
_eventDispatcher = nil;
[super removeFromSuperview];
}
-(void)reloadImage {
_image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_src]]];
_imageView.contentMode = [_contentMode integerValue];
_imageView.animatedImage = _image;
}
- (void)layoutSubviews
{
_imageView.frame = self.bounds;
[self addSubview:_imageView];
}
- (void)setSrc:(NSString *)src
{
if (![src isEqual:_src]) {
_src = [src copy];
[self reloadImage];
}
}
- (void)setContentMode:(NSNumber *)contentMode
{
if(![contentMode isEqual:_contentMode]) {
_contentMode = [contentMode copy];
[self reloadImage];
}
}
@end