怎么在React中利用Native實現(xiàn)一個進度條彈框?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

首先實現(xiàn)進度條。
import React, {
PureComponent
} from 'react';
import {
StyleSheet,
View,
Animated,
Easing,
} from 'react-native';
class Bar extends PureComponent {
constructor(props) {
super(props);
this.progress = new Animated.Value(this.props.initialProgress || 0);
}
static defaultProps = {
style: styles,
easing: Easing.inOut(Easing.ease)
}
componentWillReceiveProps(nextProps) {
if (this.props.progress >= 0 && this.props.progress !== nextProps.progress) {
this.update(nextProps.progress);
}
}
shouldComponentUpdate() {
return false;
}
update(progress) {
Animated.spring(this.progress, {
toValue: progress
}).start();
}
render() {
return (
<View style={[styles.background, this.props.backgroundStyle, this.props.style]}>
<Animated.View style={[styles.fill, this.props.fillStyle, { width: this.progress.interpolate({
inputRange: [0, 100],
outputRange: [0 * this.props.style.width, 1 * this.props.style.width],
})} ]}
/>
</View>
);
}
}
var styles = StyleSheet.create({
background: {
backgroundColor: '#bbbbbb',
height: 5,
overflow: 'hidden'
},
fill: {
backgroundColor: 'rgba(0, 122, 255, 1)',
height: 5
}
});
export default Bar;進度條的值我們用動畫實現(xiàn),避免使用state不斷去重新render渲染界面,同時設置shouldComponentUpdate返回值為false,避免重新render。
實現(xiàn)進度條彈框。
import React, {
PropTypes,
PureComponent
} from 'react';
import {
View,
StyleSheet,
Modal,
Text,
Dimensions,
TouchableOpacity
} from 'react-native';
import Bar from './Bar';
const {
width
} = Dimensions.get('window');
class ProgressBarDialog extends PureComponent {
constructor(props) {
super(props);
}
static propTypes = {
...Modal.propTypes,
title: PropTypes.string,
canclePress: PropTypes.func,
cancleText: PropTypes.string,
needCancle: PropTypes.bool
};
static defaultProps = {
animationType: 'none',
transparent: true,
progressModalVisible: false,
onShow: () => {},
onRequestClose: () => {},
onOutSidePress: () => {},
title: '上傳文件',
cancleText: '取消是',
canclePress: () => {},
needCancle: true
}
render() {
const {
animationType,
transparent,
onRequestClose,
progress,
title,
canclePress,
cancleText,
needCancle,
progressModalVisible
} = this.props;
return (
<Modal
animationType={animationType}
transparent={transparent}
visible={progressModalVisible}
onRequestClose={onRequestClose}>
<View style={styles.progressBarView}>
<View style={styles.subView}>
<Text style={styles.title}>
{title}
</Text>
<Bar
ref={this.refBar}
style={{marginLeft: 10,marginRight: 10,width:width - 80}}
progress={progress}
backgroundStyle={styles.barBackgroundStyle}
/>
<View style={styles.progressContainer}>
<Text style={styles.progressLeftText}>
{`${progress}`}%
</Text>
<Text style={styles.progressRightText}>
{`${progress}`}/100
</Text>
</View>
{needCancle &&
<View style={styles.cancleContainer}>
<TouchableOpacity style={styles.cancleButton} onPress={canclePress}>
<Text style={styles.cancleText}>
{cancleText}
</Text>
</TouchableOpacity>
</View>
}
</View>
</View>
</Modal>
);
}
}
const styles = StyleSheet.create({
progressBarView: {
flex:1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.2)'
},
barStyle: {
marginLeft: 10,
marginRight: 10,
width:width - 80
},
progressLeftText: {
fontSize: 14
},
cancleContainer: {
justifyContent: 'center',
alignItems: 'flex-end'
},
progressRightText: {
fontSize: 14,
color: '#666666'
},
barBackgroundStyle: {
backgroundColor: '#cccccc'
},
progressContainer: {
flexDirection: 'row',
padding: 10,
justifyContent: 'space-between'
},
subView: {
marginLeft: 30,
marginRight: 30,
backgroundColor: '#fff',
alignSelf: 'stretch',
justifyContent: 'center'
},
progressView: {
flexDirection: 'row',
padding: 10,
paddingBottom: 5,
justifyContent: 'space-between'
},
title: {
textAlign: 'left',
padding:10,
fontSize: 16
},
cancleButton: {
padding:10
},
cancleText: {
textAlign: 'right',
paddingTop: 0,
fontSize: 16,
color: 'rgba(0, 122, 255, 1)'
}
});
export default ProgressBarDialog;props
modal的props - 這些都是modal的屬性
animationType - 動畫類型
transparent - 是否透明
progressModalVisible - 是否可見
onShow - 彈框出現(xiàn)
onRequestClose - 彈框請求消失(比如安卓按返回鍵會觸發(fā)這個方法)
onOutSidePress - 點擊彈框外部動作
title - 彈框的標題
cancleText - 取消的文字
canclePress - 取消動作
needCancle - 是否需要取消按鈕
使用代碼
import React , {
PureComponent
} from 'react';
import {
View
} from 'react-native';
import ProgressBarDialog from './ProgressBarDialog';
class Upload extends PureComponent {
constructor(props) {
this.state = {
progress: 0,
progressModalVisible: false
};
}
refProgressBar = (view) => {
this.progressBar = view;
}
showProgressBar = () => {
this.setState({
progressModalVisible: true
});
}
dismissProgressBar = () => {
this.setState({
progress: 0,
progressModalVisible: false
});
}
setProgressValue = (progress) => {
this.setState({
progress
});
}
onProgressRequestClose = () => {
this.dismissProgressBar();
}
canclePress = () => {
this.dismissProgressBar();
}
render() {
return (
<View>
<ProgressBarDialog
ref={this.refProgressBar}
progress={this.state.progress}
progressModalVisible={this.state.progressModalVisible}
onRequestClose={this.onProgressRequestClose}
canclePress={this.canclePress}
needCancle={true}
/>
</View>
)
}
}
export default Upload;看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)建站的支持。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、建站服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前標題:怎么在React中利用Native實現(xiàn)一個進度條彈框-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article6/digdog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、做網(wǎng)站、全網(wǎng)營銷推廣、響應式網(wǎng)站、網(wǎng)站收錄、網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容