scrollView类中如何使用一个层?


比如有一个CCScrollView类,它是一个滚动控件,如何在它里面滚动自己的内容呢?
首先:创建这个CCScrillView类对象
其次:建一个CCLayer类的对象
再其次:设置了精灵。    (本例是使用6人文件名的经验,用sprintf()搞定)
再其次:把精灵加到层中。
再其次:设置层的定位点。
再其次,把层加到滚动类中。
最后,把滚动类加到当前类中。

    scrollView = CCScrollView::create(); //创建一个scrollview
    CCLayer *layer = CCLayer::create(); //创建一个层,作为滚动的内容
    char helpstr[30] = {0};
    for (int i=1;i<=6;i++)
    {
     sprintf(helpstr,"Help/Help_%02d_chs.png",i);//说明图片的文件名是按Help_01_chs.png,Help_02_chs.png,...Help_06_chs.png排列的
     CCSprite *sprite = CCSprite::create(helpstr);
     sprite->setScaleX(2.0);
     sprite->setScaleY(1.0);
     sprite->setPosition(ccp(size.width*(i-0.5),size.height/2+25));
     layer->addChild(sprite);
    }
   
    layer->setAnchorPoint(CCPointZero);
    layer->setPosition(CCPointZero);
    scrollView->setPosition(CCPointZero);
    scrollView->setContentOffset(CCPointZero);
    layer->setContentSize(CCSizeMake(480*6, 320));  //设置滚动区域的大小
    scrollView->setContentSize(CCSizeMake(480, 320));  //设置显示区域的大小  
    scrollView->setContainer(layer);                            //设置需要滚动的内容,把层加到滚动中
    scrollView->setTouchEnabled(false);                         //因为要自己实现触摸消息,所以这里设为false
    scrollView->setDelegate(this);
    scrollView->setDirection(CCScrollViewDirectionHorizontal);  //设置滚动的方向,有三种可以选择
    this->addChild(scrollView);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在React Native中,当一个ScrollView嵌套在另一个ScrollView中时,子ScrollView默认是不可以滑动的。为了让子ScrollView可以滑动,需要在父ScrollView的onTouchStart、onTouchMove和onTouchEnd事件中对事件进行处理。 具体来说,可以在父ScrollView的onTouchStart事件中记录下当前手指的位置,然后在onTouchMove事件中判断当前手指的位置是否在子ScrollView的范围内,如果是,则将事件交给子ScrollView处理;如果不是,则将事件交给父ScrollView处理。在onTouchEnd事件中,可以清除掉已经记录的手指位置信息。 以下是一个示例代码: ``` import React, { Component } from 'react'; import { ScrollView, View } from 'react-native'; class ParentScrollView extends Component { constructor(props) { super(props); this.state = { isChildScrolling: false, childScrollTop: 0, }; } handleChildScroll = (event) => { const { contentOffset } = event.nativeEvent; this.setState({ childScrollTop: contentOffset.y, }); } handleTouchStart = (event) => { const { locationY } = event.nativeEvent; if (locationY > this.state.childScrollTop) { this.setState({ isChildScrolling: true, }); } } handleTouchMove = (event) => { const { locationY } = event.nativeEvent; if (this.state.isChildScrolling) { this.childScrollView.scrollTo({ y: locationY - this.state.childScrollTop }); } } handleTouchEnd = () => { this.setState({ isChildScrolling: false, childScrollTop: 0, }); } render() { return ( <ScrollView onTouchStart={this.handleTouchStart} onTouchMove={this.handleTouchMove} onTouchEnd={this.handleTouchEnd} > <View style={{ height: 200 }}> <ScrollView ref={(ref) => { this.childScrollView = ref; }} onScroll={this.handleChildScroll} > {/* 子ScrollView的内容 */} </ScrollView> </View> {/* 父ScrollView的其他内容 */} </ScrollView> ); } } export default ParentScrollView; ``` 在上面的代码中,首先在父ScrollView的state中记录了一个isChildScrolling和childScrollTop变量,分别表示子ScrollView是否正在滑动,以及子ScrollView的当前scrollTop值。然后在父ScrollView的onTouchStart事件中,根据手指位置是否在子ScrollView的范围内,来判断是否将事件交给子ScrollView处理。在onTouchMove事件中,如果子ScrollView正在滑动,则通过scrollTo方法来滑动子ScrollView。在onTouchEnd事件中,清除掉记录的变量信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庭博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值