内容简介:最近我在项目中用到了react-native,当使用view根据屏幕自适应缩放功能的时候,我用到了自己熟悉的css transform,当想指定中线点缩放的时候,发现react-native不支持transform-origin,可能官方后面会支持吧,目前用下来还是不支持的。 报了如下错误:react-native使用了阉割版的css,不是所有的css属性都支持的,默认是不支持transform-origin的,项目中真的要用怎么办? 查阅了react-native官方issue
前言
最近我在项目中用到了react-native,当使用view根据屏幕自适应缩放功能的时候,我用到了自己熟悉的css transform,当想指定中线点缩放的时候,发现react-native不支持transform-origin,可能官方后面会支持吧,目前用下来还是不支持的。 报了如下错误:
react-native使用了阉割版的css,不是所有的css属性都支持的,默认是不支持transform-origin的,项目中真的要用怎么办? 查阅了react-native官方issue transform-origin support 然并没有找到我需要的解决方案。
探索我的项目
我用的是缩放,大体分析如下图:
因此,我trasnform scale之后,我再增加一个transformX 和transformY就可以了
代码如下:
let transformx=haoroomsViewWidth*(1-scaleRadio),transformy=haoroomsViewHeight*(1-scaleRadio)
transform: [{scale(scaleRadio)},{translateX:-transformx},{translateY:-transformy}]
关于旋转
关于transForm旋转rotate的中心点定位就麻烦一些了。当然网上有些用transformMatrix的方式。我这里也提供了一种方式, 思路和我上面一样,
rad = angle * Math.PI / 180 transformX(Math.cos(angle) * haoroomsx - Math.sin(angle) * haoroomsy) transformY(Math.sin(angle) * haoroomsx + Math.cos(angle) * haoroomsy) rotate(angle+"deg")
源码如下:仅供参考:
import { responsiveHeight, responsiveWidth, responsiveFontSize } from 'react-native-responsive-dimensions';
import BackgroundTimer from 'react-native-background-timer';
class Haorooms extend component{
constructor(props) {
super(props)
this.state = {
angle: 0
}
}
componentDidMount() {
this.progress()
}
progress(){
intervalId = BackgroundTimer.setInterval(() => {
this.setState({angle: this.state.angle +1})
}, (1000); // 1000 milliseconds
}
render () {
const dx = responsiveHeight(9.5);
const dy = responsiveHeight(9.5);
const position= {
transform: [
{
translateX: Math.cos((360 - this.state.angle) * Math.PI / 180) * dx - Math.sin((360 - this.state.angle) * Math.PI / 180) * dy
},
{
translateY: Math.sin((360 - this.state.angle) * Math.PI / 180) * dx + Math.cos((360 - this.state.angle) * Math.PI / 180) * dy
}
]
};
return(
<Animated.View style={position}>
<Text>Text move</Text>
</Animated.View>
);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Windows API编程范例入门与提高
东方人华 / 清华大学出版社 / 2004-1-1 / 38.00
本书通过大量实用、经典的范例,以Visual Basic为开发平台由浅入深地介绍了Windows API编程的基本方法和大量的实用技巧。本书采用实例带动知识点的形式,使读者快速入门并逐步得到提高。本书每节即是一个实例,操作步骤详尽,所用到的源文件均可在网站下载。读者可以按照操作步骤完成每个实例的制作,并根据自己的喜好进行修改、举一反三。 本书内容翔实,凝结了作者多年的编程经验,既适合......一起来看看 《Windows API编程范例入门与提高》 这本书的介绍吧!