forked from bradmartin/nativescript-gif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif.ios.ts
executable file
·143 lines (113 loc) · 3.75 KB
/
gif.ios.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Common = require('./gif.common');
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import { View } from "ui/core/view";
import utils = require("utils/utils")
import enums = require("ui/enums");
import definition = require("nativescript-gif");
import fs = require("file-system");
import * as typesModule from "utils/types";
global.moduleMerge(Common, exports);
declare var FLAnimatedImageView: any, FLAnimatedImage: any, NSData: any, NSString: any, NSURL: any, CGRectMake: any;
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var gif = <Gif>data.object;
if (!gif.ios) {
return;
}
gif.src = data.newValue;
}
// register the setNativeValue callback
(<proxy.PropertyMetadata>Common.Gif.srcProperty.metadata).onSetNativeValue = onSrcPropertyChanged;
export class Gif extends Common.Gif {
private _ios: FLAnimatedImageView;
private _animatedImage: FLAnimatedImage;
constructor(options?: definition.Options) {
super(options);
this._ios = FLAnimatedImageView.alloc().initWithFrame(CGRectMake(0, 0, 100, 100));
this._ios.clipsToBounds = true;
}
public onLoaded() {
super.onLoaded();
if (this.src) {
var isUrl = false;
if (this.src.indexOf("://") !== -1) {
if (this.src.indexOf('res://') === -1) {
isUrl = true;
}
}
if (!isUrl) {
var currentPath = fs.knownFolders.currentApp().path;
if (this.src[1] === '/' && (this.src[0] === '.' || this.src[0] === '~')) {
this.src = this.src.substr(2);
}
if (this.src[0] !== '/') {
this.src = currentPath + '/' + this.src;
}
// Using a local .gif
this._animatedImage = FLAnimatedImage.animatedImageWithGIFData(
NSData.dataWithContentsOfFile(
NSString.stringWithString(this.src)
)
);
} else {
// Using a URL
this._animatedImage = FLAnimatedImage.animatedImageWithGIFData(
NSData.dataWithContentsOfURL(
NSURL.URLWithString(this.src)
)
);
}
try {
this._ios.animatedImage = this._animatedImage;
this._ios.frame = CGRectMake(0, 0, 100, 100);
} catch (ex) {
console.log(ex);
}
if (isNaN(this.width) || isNaN(this.height)) {
this.requestLayout();
}
} else {
console.log("No src value detected.");
}
}
get ios(): FLAnimatedImageView {
return this._ios;
}
get _nativeView(): FLAnimatedImageView {
return this._ios;
}
get src(): any {
return this._getValue(Gif.srcProperty);
}
set src(value: any) {
this._setValue(Gif.srcProperty, value);
}
/**
* Stop playing the .gif
*/
public stop(): void {
this._ios.stopAnimating();
}
/**
* Start playing the .gif
*/
public start(): void {
this._ios.startAnimating();
}
/**
* Check if the .gif is playing.
* @returns Boolean
*/
public isPlaying(): boolean {
var isPlaying = this._ios.animatedImage.isAnimating();
return isPlaying;
}
/**
* Get the frame count for a .gif.
* @returns Number of frames.
*/
public getFrameCount(): number {
var frames = this._ios.animatedImage.frameCount
return frames;
}
}