继承ScrollView实现左右滑动分页功能

 

#ifndef __PagedScrollView__
#define __PagedScrollView__

#include <iostream>
#include "cocos2d.h"
#include "extensions/cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

class PagedScrollView : public ScrollView
{
public:
    PagedScrollView();
    ~PagedScrollView();

    static PagedScrollView* create(const Size& size);
    
public://from
    bool onTouchBegan(Touch *touch, Event *event);
    void onTouchMoved(Touch *touch, Event *event);
    void onTouchEnded(Touch *touch, Event *event);
    void onTouchCancelled(Touch *touch, Event *event);
    
private:
    void updateOffset();
    void timer(float dt);
    
private:
    Point offset_;
    clock_t timestamp_;
    bool touchable_;
};

#endif
#include "PagedScrollView.h"

PagedScrollView::PagedScrollView()
{
    timestamp_=0;
    touchable_=true;
}

PagedScrollView::~PagedScrollView()
{
}

PagedScrollView* PagedScrollView::create(const Size& size)
{
    PagedScrollView* pRet = new PagedScrollView();
    if (pRet && pRet->initWithViewSize(size,NULL))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    return pRet;
}

bool PagedScrollView::onTouchBegan(Touch *touch, Event *event)
{
    if(!touchable_)
    {
        return true;
    }
    
    timestamp_=clock()*1000/CLOCKS_PER_SEC;
    return ScrollView::onTouchBegan(touch, event);
}
void PagedScrollView::onTouchMoved(Touch *touch, Event *event)
{
    if(!touchable_)
    {
        return;
    }
    
    ScrollView::onTouchMoved(touch, event);
}
void PagedScrollView::onTouchEnded(Touch *touch, Event *event)
{
    if(!touchable_)
    {
        return;
    }
    
    ScrollView::onTouchEnded(touch, event);

    unscheduleAllSelectors();
    updateOffset();
}
void PagedScrollView::onTouchCancelled(Touch *touch, Event *event)
{
    if(!touchable_)
    {
        return;
    }
    
    ScrollView::onTouchCancelled(touch, event);
}

void PagedScrollView::updateOffset()
{
    const Size& contentSize = getContentSize();
    const Size& viewSize = getViewSize();
    Point offset = getContentOffset();
    if (offset.x == 0 || offset.x == -(contentSize.width - viewSize.width))
    {
        return;
    }
    
    if (offset.x > 0)
    {
        offset.x = 0;
        setContentOffsetInDuration(offset, 0.10);
    }
    else if (offset.x < -(contentSize.width - viewSize.width))
    {
        offset.x = -(contentSize.width - viewSize.width);
        setContentOffsetInDuration(offset, 0.10);
    }
    else
    {
        clock_t timestamp=clock()*1000/CLOCKS_PER_SEC;
        int timediff=timestamp-timestamp_;
        CCLOG("%d",timediff);
        
        if(timediff<=100)
        {
            float divideViewWidth = viewSize.width / 20;
            int diffx=offset.x-offset_.x;
            if(diffx>=divideViewWidth)
            {
                offset_.x+=viewSize.width;
            }
            else if(diffx<=-divideViewWidth)
            {
                offset_.x-=viewSize.width;
            }
        }
        else
        {
            float divideViewWidth = viewSize.width / 2;
            offset_.x = ((int)(offset.x/divideViewWidth))*viewSize.width;
        }
        
        float distance=abs(offset_.x-offset.x);
        float duration=0.6/viewSize.width*distance;
        setContentOffsetInDuration(offset_,duration);
        scheduleOnce(schedule_selector(PagedScrollView::timer), duration);
        touchable_=false;
    }
}

void PagedScrollView::timer(float dt)
{
    touchable_=true;
}

发表评论