开发者

PagingEnabled for multiple pages in UIScrollView

Edit: See the answer below.

I finally give up and come here to ask you for my problem...

I'm using a UIScrollView for a scrolling menus with li开发者_开发百科ttle icons. On each page, with paging enabled, there's an icon in the center, and 2 and a half other visible icons on the left and right. I can move from one icon to its neighbour, and that is fine, but the point is that if I do a fast scrolling, it will not move from more than 3 icons, which is the width of the screen.

What I would want is to be able to scroll on more than 3 icons, and that the magnet behaviour is only triggered when it's slowing down.

I've tried to schedule the scroll view to calculate its velocity, and set the pagingEnabled attribute to NO when it's moving fast and YES again when it's slowing down, but as soon as it is set to YES, the view comes back very fast at its original position, as if it was not detecting that I had brought it to a new page. Would anyone know why it does this? And if I have a way to tell the view "ok, now the paging is enabled but look, you're 15 pages later. Just center on the current page, don't come back at the beginning."

Here's my update function (if it can help):

-(void)update:(ccTime)dt { float velocity = fabsf((self.previousOffset-self.scrollView.contentOffset.y)/dt); self.previousOffset = self.scrollView.contentOffset.y; CCLOG(@"Velocity: %f", velocity); if(self.scrollView.pagingEnabled) { if(velocity > 100) { self.scrollView.pagingEnabled = NO; } } else { if(velocity < 100) { self.scrollView.pagingEnabled = YES; } } }


I finally found a solution, which was pretty obvious, but that I did not see at the beginning, by using setContentOffset on the scrollView.

Here is the new update function:

-(void)update:(ccTime)dt

{

    float velocity = 1000;
    if(self.previousOffset)
    {
        velocity = fabsf((self.previousOffset-self.scrollView.contentOffset.y)/dt);
    }
    self.previousOffset = self.scrollView.contentOffset.y;

    if(velocity < 300)
    {

        CGSize screenSize = [[CCDirector sharedDirector] winSize];
        float halfScreen = screenSize.width/2;
        CCLayer *panel = (CCLayer *)[self getChildByTag:1];
        SQScrollViewMenu *menu = (SQScrollViewMenu *)[panel getChildByTag:1];
        SQMissionItem *currentItem = (SQMissionItem *)[menu getChildByTag:currentPage];

        float contentOffsetY = [self.scrollView contentOffset].y;
        CCLOG(@"Currentpage: %i ; currentoffsetY: %f", currentPage, contentOffsetY);
        float distance = contentOffsetY + [currentItem position].x - halfScreen + panel.position.x + menu.position.x + panel.parent.position.x - 60;
        CCLOG(@"Which gives a distance of: %f", distance);

        [self.scrollView setContentOffset:CGPointMake(0, distance) animated:YES];
        self.previousOffset = 0;
        [self unschedule:@selector(update:)];
        CCLOG(@"Is unscheduled");
    }
}

And it is almost working... at least, it is on simulator. But as soon as I try it on my iPhone 4, it does not work anymore. It always go into that update function, but 7 times among 8, it just blocks the scrollView as it is and does not drags it back to the position I give it... but sometimes it does.

Would anyone have an idea? I found similar issues on the net, but none of them could resolve this...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜