我想旋转一个Angular 为-90degText分量.组件应从左角旋转,而不是从中间旋转.我使用以下代码:

render() {
     return (
            <View style = {{flex : 1}}>
                <View style={styles.sidebar}>
                    <Text style={styles.lblSideBar} numberOfLines = {1} >DUMMY TEXT</Text>
                </View>
            </View>);
}

const styles = StyleSheet.create({
  sidebar : {
    flex : 1,
    flexDirection : 'row',
    alignItems : 'flex-end',
    justifyContent : 'center',
    width : 30,
  },
  lblSideBar : {
      bottom : 20,
      transform : [{rotate : '-90deg'}],
  }
});

我希望底部空间为20px,但由于旋转是从中心进行的(默认情况下),文本在旋转后覆盖底部区域.请建议如何实现这一点?

推荐答案

我刚刚做了一个withAnchorPoint函数,让transform在react native中更容易使用锚点.https://github.com/sueLan/react-native-anchor-point

你可以这样使用它:

import { withAnchorPoint } from 'react-native-anchor-point';

getTransform = () => {
    let transform = {
        transform: [{ perspective: 400 }, { rotateX: rotateValue }],
    };
    return withAnchorPoint(transform, { x: 0.5, y: 0 }, { width: CARD_WIDTH, height: CARD_HEIGHT });
};

<Animated.View style={[styles.blockBlue, this.getTransform()]} />

我还为此写了一篇文章.

enter image description here

下面是一些棘手的代码,用于设置视图的定位点或轴点.

  1. 在x轴、y轴、z轴上将视图平移x、y、z轴
  2. 应用旋转
  3. 在x轴、y轴、z轴上将视图向后平移-x、-y、-z
          translateX: -w / 2
          rotateY
          translateX: w / 2

这意味着将锚点设置为(0,0.5).我们将其应用于变换样式中,如下所示

   const transform = {
      transform: [
          {translateX: -w / 2}, 
          rotateY, 
          {translateX: w / 2}
      ]
    }
    return (   
        <Animated.View style={transform}></Animated.View>
    )
  }

          translateX: w / 2
          rotateY
          translateX: -w / 2

这意味着将锚点设置为(1,0.5)

          translateX: -w / 2
          translateY: -h / 2
          rotateZ
          translateX: w / 2
          translateY: h / 2

这意味着将锚点设置为(0,0)

          translateX: w / 2
          translateY: h / 2
          rotateZ
          translateX: -w / 2
          translateX: -h / 2

这意味着将锚点设置为(1,1)

在iOS中,它被称为锚点.About the anchorPoint

layer.anchorPoint = CGPointMake(0, 0)

在安卓系统中,它被称为轴心点.

  viewToMove.setPivotX(0);
  viewToMove.setPivotY(0);

React-native相关问答推荐

无法读取 @react-native-async-storage/async-storage 的未定义属性setItem

react native调用fetch后如何使用Async Storage在本地保存数据?

从 React 原生 Js 代码调用 Android Native UI 组件方法

如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL

React-native android 样式化 textInput

错误:安装 react-native-elements 后无法识别字体系列 Material Design 图标?

如何在 React Native 中按下按钮时更改文本值?

React native base headers for ios not found

使用 Jest 测试 React Native 应用程序

在 react-native-maps 中渲染多个标记

HighLight / ScrollTo - React-Native 上的 ListView、ScrollView

使用来自另一个页面的 fetch 调用函数返回结果值

如何将 React Native Promise 连接到 Swift

使用 typescript react-native - 如何使用来自 @react-navigation/native 的 useRoute 和 typescript

React Native - 如何从推送通知中打开路由

com.facebook.react.bridge.readablenativemap 不能转换为 java.lang.string

降级 react-native 的适当机制

运行 expo start 时如何禁用在浏览器中打开 DevTools?

ReactNative Flatlist - RenderItem not working

无法在react-native中分配给# 的只读属性props