Touchable系列组件

学习资料
博客:Touchable系列组件
TouchableHighlightTouchableOpacity

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

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity
} from 'react-native';

class TouchableDemo extends Component {
  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity activeOpacity={0.5}>
          <View style={styles.innerViewStyle}>
            <Text>我是文本,但可点击</Text>
          </View>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  innerViewStyle: {
    backgroundColor: 'red',
  }
});

AppRegistry.registerComponent('TouchableDemo', () => TouchableDemo);

这里写图片描述

不透明触摸 TouchableOpacity
该组件封装了响应触摸事件;当点击按下的时候,该组件的透明度会降低。
常用属性:activeOpacity 设置当用户触摸的时候,组件的透明度

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

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  AlertIOS
} from 'react-native';

// ES5新建组件的写法
var TouchableDemo = React.createClass({
  getInitialState(){
    return {
      title:'不透明触摸'
    }
  },

  render() {
    return (
        <View style={styles.container}>
          <TouchableOpacity
              activeOpacity={0.5}
              onPress={()=>this.activeEvent('点击')}
              onPressIn={()=>this.activeEvent('按下')}
              onPressOut={()=>this.activeEvent('抬起')}
              onLongPress={()=>this.activeEvent('长按')}
          >
            <View style={styles.innerViewStyle}>
              <Text>点击事件</Text>
            </View>
          </TouchableOpacity>
          <View style={{marginTop:20}}>
            <Text>状态: {this.state.title}</Text>
          </View>
        </View>
    );
  },

  // 鼠标触发这个函数
  activeEvent(event){
    // 修改title
    this.setState({
      title:event
    })
  }
});

// ES6的写法
// class TouchableDemo extends Component {
//
// }

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  innerViewStyle: {
    backgroundColor: 'red',
  }
});

AppRegistry.registerComponent('TouchableDemo', () => TouchableDemo);

这里写图片描述

补充:
上面我们用的是ES5的写法,下面我们来用ES6的写法实现:

// ES6的写法
class TouchableDemo extends Component {
  // 构造
  constructor(props) {
    super(props);
    // 初始状态
    this.state = {title:'不透明触摸'};
  }

  render() {
    return (
        <View style={styles.container}>
          <TouchableOpacity
              activeOpacity={0.5}
              onPress={()=>this.activeEvent('点击')}
              onPressIn={()=>this.activeEvent('按下')}
              onPressOut={()=>this.activeEvent('抬起')}
              onLongPress={()=>this.activeEvent('长按')}
          >
            <View style={styles.innerViewStyle}>
              <Text>点击事件</Text>
            </View>
          </TouchableOpacity>
          <View style={{marginTop:20}}>
            <Text>状态: {this.state.title}</Text>
          </View>
        </View>
    );
  }

  // 鼠标触发这个函数
  activeEvent(event){
    // 修改title
    this.setState({
      title:event
    })
  }
}

对比2种写法的区别。
这里边还需要学习 React Native组件生命周期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值