Taro钟表组件、兼容h5和ReactNative

后续会支持小程序
先上效果图
H5端:
在这里插入图片描述
RN端:我用的是Iphone8plus
在这里插入图片描述
父组件:

import Taro, { Component } from '@tarojs/taro'
import TimeClockh5 from './../../../h5/components/home/timeClock'
import TimeClockRN from './../../../reactNative/components/home/timeClock'
// import { getMinutes,getSecond,getHours } from '@/commons/date'


function getMinutes(){  //获取当前分钟数
    var myDate = new Date();
    return myDate.getMinutes()
}
function getHours(){    //获取当前的小时数
    var myDate = new Date();
    return myDate.getHours()
}
type stateType = {
    time:number
}
type propsType = {

}
interface TimeClock{
    state:stateType,
    props:propsType
}
class TimeClock extends Component{
    constructor(props){
        super(props)
        this.state = {
            time:this.getDeg()
        }
    }

    componentWillMount(){
        setInterval(()=>{   //每分钟刷新一次指针
            this.setState({
                time:this.getDeg()
            })
        },60000)
        
    }
    getDeg(){   //计算当前分钟内的度数
        let dayMinutes = getHours() * 60 + getMinutes();  //获取当前一天已经过去的分钟数量
        let totalMinutes = 24 * 60; //当前天数的总分钟数量
        let bili = dayMinutes/totalMinutes; //计算出比例
        return 360*bili;
    }

    render(){
        const {
            time:time
        } = this.state
        return(
                process.env.TARO_ENV === 'rn'?
                <TimeClockRN Minutes={time}></TimeClockRN>:
                <TimeClockh5 Minutes={time}></TimeClockh5>
        )
    }
}
export default TimeClock;

RN端子组件:(好像用不用Animated都可以)

import Taro, { Component } from '@tarojs/taro'
import { View, Text,Image } from '@tarojs/components'
import { Animated } from 'react-native';
import styles from './timeClock.scss'

type propsType = {
    Minutes:number
}
type stateType = {
    boxWidth:any,   //指针盒子的宽度
    boxHeight:any,  //长度
    boxMarginTop:any,
    boxMarginLeft:any,
    boxTransform:any,   //旋转角度
}
interface TimeClock{
    props:propsType,
    state:stateType
}
class TimeClock extends Component{
    constructor(props){
        super(props)
        this.state = {
            boxWidth:new Animated.Value(Taro.pxTransform(13)),  //pxTransform  Taro提供的运行时样式转换
            boxHeight:new Animated.Value(Taro.pxTransform(488)),
            boxMarginTop:new Animated.Value(Taro.pxTransform(206)),
            boxMarginLeft:new Animated.Value(Taro.pxTransform(443)),
            boxTransform:"0deg",
        }
    }

    componentDidMount(){
        this.setState({
            boxTransform:this.props.Minutes + "deg"
        })
    }
    componentWillReceiveProps(props){
        this.setState({
            boxTransform:this.props.Minutes + "deg"
        })
    }
    render(){
        const {
            boxWidth:boxWidth,
            boxHeight:boxHeight,
            boxMarginTop:boxMarginTop,
            boxMarginLeft:boxMarginLeft,
            boxTransform:boxTransform
        } = this.state
        return(
            <View className={styles.bigBox} style={styles.bigBox}>
                <Image className={styles.biaoBackImg} style={styles.biaoBackImg} src={require('./../../../assets/icon/timeBlock.png')}></Image>
                <Animated.View style={{
                    width: boxWidth,
                    height: boxHeight,
                    marginTop: boxMarginTop,
                    marginLeft: boxMarginLeft,
                    transform:[
                        {rotateZ:boxTransform}
                    ]
                }}>
                    <Image className={styles.zhiZhengIcon} style={styles.zhiZhengIcon} src={require('./../../../assets/icon/zhizhengIcon.png')}></Image>
                </Animated.View>
            </View>
        )
    }
}
export default TimeClock;

RN端scss样式:

.bigBox{
    position: absolute;
    top: 248px;
    left: 90px;
    width: 900px;
    height: 900px;
    z-index: 999;
}
.biaoBackImg{
    position: absolute;
    width: 900px;
    height: 900px;
    top: 0;
    left: 0;
}
.zhiZhengIcon{
    width: 13px;
    height: 244px;
}

h5端组件:

import Taro, { Component } from '@tarojs/taro'
import { View, Text,Image } from '@tarojs/components'
import styles from './timeClock.scss'


type propsType = {
    Minutes:number
}
type stateType = {
    boxTransform:any,   //旋转角度
}
interface TimeClock{
    props:propsType,
    state:stateType
}
class TimeClock extends Component{
    constructor(props){
        super(props)
        this.state = {
            boxTransform:"0deg",    //旋转角度
        }
    }
    componentDidMount(){
        this.setState({
            boxTransform:this.props.Minutes + "deg"
        })
    }
    componentWillReceiveProps(props){
        this.setState({
            boxTransform:this.props.Minutes + "deg"
        })
    }

    render(){
        const {
            boxTransform:boxTransform
        } = this.state
        return(
            <View className={styles.bigBox} style={styles.bigBox}>
                <Image className={styles.biaoBackImg} style={styles.biaoBackImg} src={require('./../../../assets/icon/timeBlock.png')}></Image>
                <View className={styles.zhizhenBox} style={{"transform": "rotateZ(" + boxTransform + ")"}}>
                    <Image className={styles.zhiZhengIcon} style={styles.zhiZhengIcon} src={require('./../../../assets/icon/zhizhengIcon.png')}></Image>
                </View>
            </View>
        )
    }
}
export default TimeClock;

h5端scss样式:

.bigBox{
    position: absolute;
    top: 248px;
    left: 90px;
    width: 900px;
    height: 900px;
    z-index: 999;
}
.biaoBackImg{
    position: absolute;
    width: 900px;
    height: 900px;
    top: 0;
    left: 0;
}
.zhizhenBox{
    width: 13px;
    height: 488px;
    margin-top: 206px;
    margin-left: 443.5px;
    // transform: rotateZ(90deg);
}
.zhiZhengIcon{
    width: 13px;
    height: 244px;
}

前天可算体会一下删库的感觉
有什么不完善的地方说一声

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值