美文网首页javascriptreact-nativereact-native开发
react-native 仿网易云音乐旋转唱片动画

react-native 仿网易云音乐旋转唱片动画

作者: smartphp | 来源:发表于2016-11-15 00:48 被阅读2608次

一直觉得网易云音乐的播放界面很酷,现在利用react-native 动画实现这个界面.要点有两个,1图片要变园使用borderRadius=1/2height.2动画处理改变图片的旋转角度

Untitled.gif

使用方法,直接react-native init 一个项目,然后把这段代码复制到入口文件中
下面看代码

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * 
 */


import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Animated,   //使用Animated组件
    Easing,     //引入Easing渐变函数
} from 'react-native';
 
class ReactNativeDemo extends Component {
    constructor(props:any) {
        super(props);
        //使用Animated.Value设定初始化值(缩放度,角度等等)
        this.state = {
            bounceValue: new Animated.Value(1), //你可以改变这个值看
//看效果是什么
            rotateValue: new Animated.Value(0),//旋转角度的初始值
        };
    }
 
    componentDidMount() {
        //在初始化渲染执行之后立刻调用动画执行函数
        this.startAnimation();
    }
 
    startAnimation() {
        this.state.bounceValue.setValue(1);//和上面初始值一样,所以
//弹动没有变化
        this.state.rotateValue.setValue(0);
        Animated.parallel([
            //通过Animated.spring等函数设定动画参数
            //可选的基本动画类型: spring, decay, timing
            Animated.spring(this.state.bounceValue, {
                toValue: 1,      //变化目标值,也没有变化
                friction: 20,    //friction 摩擦系数,默认40
            }),
            Animated.timing(this.state.rotateValue, {
                toValue: 1,  //角度从0变1
                duration: 15000,  //从0到1的时间
                easing: Easing.out(Easing.linear),//线性变化,匀速旋转
            }),
            //调用start启动动画,start可以回调一个函数,从而实现动画循环
        ]).start(()=>this.startAnimation());
    }
 
    render() {
        return (
            <View style={styles.container}>
               //插入一张图片
                <Animated.Image source={require('./img/pic.jpg')}
                                style={{width:150,
                                height: 150,borderRadius:75, //图片变园
                                transform: [
                                //将初始化值绑定到动画目标的style属性上
                                {scale: this.state.bounceValue},
                                //使用interpolate插值函数,实现了从数值单位的映
//射转换,上面角度从0到1,这里把它变成0-360的变化
                                {rotateZ: this.state.rotateValue.interpolate({
                                inputRange: [0,1],
                                outputRange: ['0deg', '360deg'],
                                })},
                     ]}}>
                </Animated.Image>
 
            </View>
        );
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
});
 


AppRegistry.registerComponent('AnimatinoDemo', () => ReactNativeDemo);

相关文章

网友评论

  • 呼呼哥:请问这个动画怎么停止啊?(页面pop之后还在后台一直运行,退出之后还在后台一致运行,其他同步进程会阻塞)
    417f78ce376d:@smartphp 其实并不会涉及到异步的问题,这个旋转图片的很大一部分都是用在音乐播放界面的唱片旋转,本就是需要在后台运行的。不过你这么写的话在暂停该动画的时候不好弄,我目前也在想解决办法。
    目前遇见的问题是:在动画过程中调用stop会导致动画重新运行,而不是停止了
    smartphp: @Sun_翁航 现在这个问题还不太好解决,涉及到异步问题,可以关注一下我最近在翻译的几篇redux-saga的文章,在使用saga解决这个问题可能容易一点。但是本文重点并不是在这里,只是想实现一个动画效果。你提的问题是新的要求了。

本文标题:react-native 仿网易云音乐旋转唱片动画

本文链接:https://www.haomeiwen.com/subject/rlekpttx.html