Repository navigation
Expand file tree
/
Copy pathProgress.js
More file actions
executable file
·168 lines (155 loc) · 4.19 KB
/
Copy pathProgress.js
File metadata and controls
executable file
·168 lines (155 loc) · 4.19 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright © SuperMap. All rights reserved.
Author: Yang Shanglong
E-mail: yangshanglong@supermap.com
*/
import React, { Component } from 'react'
import { View, Animated, Easing } from 'react-native'
import PropTypes from 'prop-types'
import styles from './styles'
export default class Progress extends Component {
static propTypes = {
...View.propTypes,
//当前进度
progress: PropTypes.number,
//second progress进度
buffer: PropTypes.number,
//进度条颜色
progressColor: PropTypes.string,
//buffer进度条颜色
bufferColor: PropTypes.string,
//进度动画时长
progressAniDuration: PropTypes.number,
//buffer动画时长
bufferAniDuration: PropTypes.number,
}
static defaultProps = {
//进度条颜色
progressColor: 'yellow',
//buffer进度条颜色
bufferColor: 'rgba(255,0,0,0.7)',
//进度条动画时长
progressAniDuration: 300,
//buffer进度条动画时长
bufferAniDuration: 300,
}
// eslint-disable-next-line
componentWillMount() {
this.progress = this.props.progress || 0
this.buffer = this.props.buffer
}
constructor(props) {
super(props)
this._progressAni = new Animated.Value(0)
this._bufferAni = new Animated.Value(0)
}
_onLayout = ({
nativeEvent: {
layout: { width, height },
},
}) => {
//防止多次调用,当第一次获取后,后面就不再去获取了
if (width > 0 && this.totalWidth !== width) {
//获取progress控件引用
let progress = this._getProgress()
//获取buffer控件引用
let buffer = this._getBuffer()
//获取父布局宽度
this.totalWidth = width
//给progress控件设置高度
progress.setNativeProps({
style: {
height: height,
},
})
//给buffer控件设置高度
buffer.setNativeProps({
style: {
height: height,
},
})
//开始执行进度条动画
this._startAniProgress(this.progress)
//开始执行buffer动画
this._startAniBuffer(this.buffer)
}
}
_startAniProgress = progress => {
if (progress >= 0 && this.totalWidth !== 0) {
Animated.timing(this._progressAni, {
toValue: progress * this.totalWidth,
duration: this.props.progressAniDuration,
easing: Easing.linear,
}).start()
}
}
_startAniBuffer = buffer => {
if (buffer >= 0 && this.totalWidth !== 0) {
Animated.timing(this._bufferAni, {
toValue: buffer * this.totalWidth,
duration: this.props.bufferAniDuration,
}).start()
}
}
_getProgress = () => {
if (typeof this.mProgress.refs.node !== 'undefined') {
return this.mProgress.refs.node
}
return this.mProgress._component
}
_getBuffer = () => {
if (typeof this.mBuffer.refs.node !== 'undefined') {
return this.mBuffer.refs.node
}
return this.mBuffer._component
}
render() {
return (
<View
style={[styles.container, this.props.style]}
onLayout={this._onLayout}
>
<Animated.View
ref={ref => (this.mProgress = ref)}
style={{
position: 'absolute',
width: this._progressAni,
backgroundColor: this.props.progressColor,
}}
/>
<Animated.View
ref={ref => (this.mBuffer = ref)}
style={{
position: 'absolute',
width: this._bufferAni,
backgroundColor: this.props.bufferColor,
}}
/>
</View>
)
}
}
Object.defineProperty(Progress.prototype, 'progress', {
set(value) {
if (value >= 0 && this._progress !== value) {
this._progress = value
this._startAniProgress(value)
}
},
get() {
return this._progress
},
enumerable: true,
})
Object.defineProperty(Progress.prototype, 'buffer', {
set(value) {
if (value >= 0 && this._buffer !== value) {
this._buffer = value
this._startAniBuffer(value)
}
},
get() {
return this._buffer
},
enumerable: true,
})