

React Native中的动画过渡
source link: http://www.10tiao.com/html/216/201806/2652561335/2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近我在为下一个动画设计试着找新灵感,并在一个网站上看到了一些。我很好奇能否用React Native实现这些过渡。
我们可以看到上图包含了几个动画,工具栏(展示/隐藏),底边条(展示/隐藏),移动选定物品,隐藏所有其他物品,展示物品细节等等。
动画的时间轴
这个过渡的难点是让这些动画同步。我们不能真的取消挂载列表页并展示详情页面,因为我们需要等待所有的动画放完。
同样我也喜欢简洁,易于维护的代码。如果你曾经尝试过为你的项目添加动画,代码经常会很乱,充满各种辅助参数与头疼的计算。这就是为什么我会推荐react-native-motion.。
react-native-motion的思想
你有留意到工具栏标题的动画吗?你只是移动标题一点,并把不透明度在0/1间变化,并不是多复杂的操作。但是为了这些,你要写这样的代码,这还没考虑到你实际为这个组件编写UI。
class TranslateYAndOpacity extends PureComponent {
constructor(props) {
// ...
this.state = {
opacityValue: new Animated.Value(opacityMin),
translateYValue: new Animated.Value(translateYMin),
};
// ...
}
componentDidMount() {
// ...
this.show(this.props);
// ...
}
componentWillReceiveProps(nextProps) {
if (!this.props.isHidden && nextProps.isHidden) {
this.hide(nextProps);
}
if (this.props.isHidden && !nextProps.isHidden) {
this.show(nextProps);
}
}
show(props) {
// ...
Animated.parallel([
Animated.timing(opacityValue, { /* ... */ }),
Animated.timing(translateYValue, { /* ... */ }),
]).start();
}
hide(props) {
// ...
Animated.parallel([
Animated.timing(opacityValue, { /* ... */ }),
Animated.timing(translateYValue, { /* ... */ }),
]).start();
}
render() {
const { opacityValue, translateYValue } = this.state;
const animatedStyle = {
opacity: opacityValue,
transform: [{ translateY: translateYValue }],
};
return (
<Animated.View style={animatedStyle}>{this.props.children}</Animated.View>
);
}
}
现在让我们看看怎么用react-native-motion实现它。我知道经常有些动画很特别,不过React Native提供了一个强大的动画API。不管怎么说,拥有一个基础动画库总是好的。
import { TranslateYAndOpacity } from 'react-native-motion';
class ToolbarTitle extends PureComponent {
render() {
return (
<TranslateYAndOpacity duration={250}>
<View>
// ...
</View>
</TranslateYAndOpacity>
);
}
}
共享元素
这个动画最大的问题是移动从列表中选择的物品。这个物品被列表页(ListPage)与详情页(DetailPage)共享,怎么在元素事实上并没有绝对地放置的时候,把一个物品从列表移动到详情页的顶部?使用React Native动作就会简单做到。
// List items page with source of SharedElement
import { SharedElement } from 'react-native-motion';
class ListPage extends Component {
render() {
return (
<SharedElement id="source">
<View>{listItemNode}</View>
</SharedElement>
);
}
}
我们定义了列表页中SharedElement的源要素。我们对详情页中的目的元素做类似的事,这样就能知道我们要把共享元素移动到的位置。
// Detail page with a destination shared element
import { SharedElement } from 'react-native-motion';
class DetailPage extends Component {
render() {
return (
<SharedElement sourceId="source">
<View>{listItemNode}</View>
</SharedElement>
);
}
}
重点在哪里?
我们怎么把一个相对放置的元素从一个页面移动到另一个呢?很可惜这并不能做到。实际上SharedElement是这样运行的:
为源元素获取一个位置
获取目的元素的位置(显然,没有这步动画就无法开始)
为共享元素创建一个克隆(重点!)
在屏幕上渲染一个新的图层
渲染这个克隆元素,使它覆盖源元素(在它的位置上)
开始移动到目标位置
一旦到达目标位置,移除克隆元素
你可以想象同时存在3个同样React Node的元素。因为在这个动画中列表页被详情页覆盖。这就是为什么我们可以看到全部3元素。但是我们制造了一个好像移动了最初物品的假象。
SharedElement时间轴
你可以看到A点和B点,它们之间的区域代表移动过程中的时间。你还可以看到SharedElement激活了一些有用的事件。在本例中,我们使用WillStart和DidFinish事件,根据你的实际需要为源和目标元素设置不透明度,当开始向目标移动时为0,一旦动画完成把目标元素重新设为1。
相关推荐:
Recommend
-
114
Vue 常用的过渡 2017.11.05 10:58 字数 1826 喜欢 28 评论 3 ...
-
81
这篇文章发布于 2018年07月22日,星期日,21:14,归类于canvas相关。 阅读 27 次, 今日 27 次 byzhangxinxu from https://www.zhangxinxu.com/wordpress/?p=7809...
-
46
对于移动端的Web单页应用来说,为了达到媲美原生应用的效果,页面过渡动画是必不可少的。常用的页面过渡动画包括: 位移——当前页向左侧或右侧水平移出可视区,下一页由反方向移入可视区。 不透明度变化——当前页...
-
54
在开发中,我们想要给一个组件的显示和消失添加某种过渡动画,可以很好的增加用户体验。 当然,我们可以通过原生的CSS来实现这些过渡动画,但是React社区为我们提供了react-transition-group用来完成过渡动画。
-
8
这个问题源自于掘金上的一个留言,一个朋友问到,为什么我下面这段代码的高度过渡动画失效了? 伪代码大概是这样: { height: unset; transition: all 0.3s linear; will-change: height; &.up...
-
7
CSS3中出现很多新的特性,下面就讲一下其中比较好玩的3D操作,过渡和动画效果; 过渡(transition) 过渡就是使瞬间的样式变化,按照一定方式变得缓慢平缓; 例如鼠标划过超链接时...
-
5
Vue过渡和动画效果展示(案例、GIF动图演示、附源码) 本篇随笔主要写了Vue过渡...
-
4
在 android5.0 以上版本中,google 为我们提供了几种 activity 切换的过渡动画,目的是为了让 activity 切换转场更加美观,而在 android5.0 之前的 activity 切换显得生硬。虽然可以自定义给 activity 增添动画效果,但是效果也不尽如意。而 androi5.x 提供的切换动...
-
5
V2EX › React React 中怎么快速实现 /t/897332 这种过渡动画?
-
3
微软 Windows 11 虚拟桌面过渡动画回归,并添加切换提示 作者:汪淼 2023-05-04 07:20:56 根据海外网友 PhantomOfEarth 发现的最新测试内容,微软正在为 Windows 11 改进虚拟桌面,在预览版 Build 23440 和 Build...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK