Duplicating Android ViewPager type functionality with menus on the iPhone

不想你离开。 提交于 2019-12-06 05:46:51

I have to do this for a project as well. I've got it mostly working. Minus the animation when scrolling it. I've started a project on github for this. I will be adding to it as I go, but in the mean time this should be a good jumping off point. The demo can be found here, https://github.com/btate/BTViewPager

I think the best approach would be to create a paging UIScrollView. Then place both the navigation and main content UIScroll in one UIView/page. When you swipe, the pages will lock into place. It looks like the navigation is animated a bit, but I would assume that Google used this approach.

Here's a tutorial with sample code to give you exactly what you'd need for paging scroll view.

http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

To accomplish the navigation labels, I'd subclass UIView and add the view right above the main scroll view. Then tell the view to reorganise the labels. Probably in `- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

Here's something I quickly wrote to demonstrate what I mean. This would be in a subclass of UIView

- (void)addLablesForItems:(NSArray *)items
{

    for (NSString *s in items)
    {
        UILabel *l = [[UILabel alloc] initWithFrame:CGRectZero];
        l.text = s;
        [self addSubview:l];
    }

    [self layoutSubviews];
}

- (void)layoutSubviews
{

    for (UIView *l in [self subviews])
    {
        if ([l isKindOfClass:[UILabel class]])
        {
            int index = [[self subviews] indexOfObject:l];
            if (index == 0)
            {
                l.frame = CGRectMake(0, 0, 20, 20);
            }
            else
            {
                UILabel *previousL = (UILabel *)[[self subviews] objectAtIndex:index -1];

                l.frame = CGRectMake(previousL.frame.origin.x+previousL.frame.size.width, 0, 20, 20);
            }
        }
    }
}    

- (void)shiftLabels:(int)direction
{
    //0 is left
    //1 is right    
    for (UIView *l in [self subviews])
    {
        if ([l isKindOfClass:[UILabel class]])
        {
            if (direction == 0)
            {
                int newOrigin = l.frame.origin.x - l.frame.size.width;
                if (newOrigin < 0)
                {
                    newOrigin = self.frame.size.width - l.frame.size.width;
                }
                [UIView animateWithDuration:.5 animations:^{

                    l.frame = CGRectMake(newOrigin, 0, 20, 20);
                }];
            }
            if (direction == 1)
            {
                int newOrigin = l.frame.origin.x + l.frame.size.width;
                if (newOrigin < 0)
                {
                    newOrigin = self.frame.origin.x;
                }

                [UIView animateWithDuration:.5 animations:^{
                    l.frame = CGRectMake(newOrigin, 0, 20, 20);                    
                }];
            }
        }
    }
}

`

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!