zl程序教程

您现在的位置是:首页 >  其他

当前栏目

React Native手势密码组件

密码组件密码React native 手势
2023-09-27 14:28:58 时间

https://juejin.im/entry/5a095435518825619a022f0d

这次介绍的React Native手势密码组件为react-native-gesture-password,纯JavaScript实现,同时支持iOS和安卓平台。

效果图

安装

npm install react-native-gesture-password--save

属性

所有的属性都是可选的。现在列举几个重要属性。

message (string)

给用户的提示信息,如请输入手势密码,手势密码不准确等此类消息。

status (string)

状态为:'normal', 'right' or 'wrong’.验证手势密码是否准确是需要自己在onEnd事件中来判断的。

onStart (function)

当用户开始输入手势密码时触发。

onEnd (function)

当用户结束输入手势密码时触发。

示例


 
 
  1. var React = require('react');

  2. var {

  3.    AppRegistry,

  4.    } = require('react-native');

  5. var PasswordGesture = require('react-native-gesture-password');

  6. var Password1 = '';

  7. var AppDemo = React.createClass({

  8.    // Example for check password

  9.    onEnd: function(password) {

  10.        if (password == '123') {

  11.            this.setState({

  12.                status: 'right',

  13.                message: 'Password is right, success.'

  14.            });

  15.            // your codes to close this view

  16.        } else {

  17.            this.setState({

  18.                status: 'wrong',

  19.                message: 'Password is wrong, try again.'

  20.            });

  21.        }

  22.    },

  23.    onStart: function() {

  24.        this.setState({

  25.            status: 'normal',

  26.            message: 'Please input your password.'

  27.        });

  28.    },

  29.    onReset: function() {

  30.        this.setState({

  31.            status: 'normal',

  32.            message: 'Please input your password (again).'

  33.        });

  34.    },

  35.    // Example for set password

  36.    /*

  37.    onEnd: function(password) {

  38.        if ( Password1 === '' ) {

  39.            // The first password

  40.            Password1 = password;

  41.            this.setState({

  42.                status: 'normal',

  43.                message: 'Please input your password secondly.'

  44.            });

  45.        } else {

  46.            // The second password

  47.            if ( password === Password1 ) {

  48.                this.setState({

  49.                    status: 'right',

  50.                    message: 'Your password is set to ' + password

  51.                });

  52.                Password1 = '';

  53.                // your codes to close this view

  54.            } else {

  55.                this.setState({

  56.                    status: 'wrong',

  57.                    message:  'Not the same, try again.'

  58.                });

  59.            }

  60.        }

  61.    },

  62.    onStart: function() {

  63.        if ( Password1 === '') {

  64.            this.setState({

  65.                message: 'Please input your password.'

  66.            });

  67.        } else {

  68.            this.setState({

  69.                message: 'Please input your password secondly.'

  70.            });

  71.        }

  72.    },

  73.    */

  74.    getInitialState: function() {

  75.        return {

  76.            message: 'Please input your password.',

  77.            status: 'normal'

  78.        }

  79.    },

  80.    render: function() {

  81.        return (

  82.            <PasswordGesture

  83.                ref='pg'

  84.                status={this.state.status}

  85.                message={this.state.message}

  86.                onStart={() => this.onStart()}

  87.                onEnd={(password) => this.onEnd(password)}

  88.                />

  89.        );

  90.    }

  91. });

  92. AppRegistry.registerComponent('AppDemo', () => AppDemo);

组件地址

详细说明和示例代码请查看GitHub:https://github.com/Spikef/react-native-gesture-password。点击阅读原文可以直接打开GitHub。