weixin_39966020
weixin_39966020
2021-01-12 14:17

Set start index of page menu

Is it possible to specify which viewcontroller to be presented first? For instance if I have three viewcontrollers:

v1, v2, v3

Is it possible for me to say that when the view opens it opens v2, instead of v1?

该提问来源于开源项目:PageMenu/PageMenu

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

10条回答

  • weixin_39902598 weixin_39902598 4月前

    +1

    点赞 评论 复制链接分享
  • weixin_39785524 weixin_39785524 4月前

    i did fork and created new parameter (IndexInitialController(Int)), but not is perfect because it load the smallest controllers that initial index. https://github.com/hellodevapps/PageMenu

    点赞 评论 复制链接分享
  • weixin_39886841 weixin_39886841 4月前

    add this to CAPSPageMenu class:

    
    public func setStartIndexToPage(index: Int) {
        if index >= 0 && index < controllerArray.count {
            // Update page if changed
            if index != currentPageIndex {
                startingPageForScroll = index
                lastPageIndex = currentPageIndex
                currentPageIndex = index
                didTapMenuItemToScroll = true
    
                // Add pages in between current and tapped page if necessary
                let smallerIndex : Int = lastPageIndex < currentPageIndex ? lastPageIndex : currentPageIndex
                let largerIndex : Int = lastPageIndex > currentPageIndex ? lastPageIndex : currentPageIndex
    
                if smallerIndex + 1 != largerIndex {
                    for i in (smallerIndex + 1)...(largerIndex - 1) {
                        if pagesAddedDictionary[i] != i {
                            addPageAtIndex(i)
                            pagesAddedDictionary[i] = i
                        }
                    }
                }
    
                addPageAtIndex(index)
    
                // Add page from which tap is initiated so it can be removed after tap is done
                pagesAddedDictionary[lastPageIndex] = lastPageIndex
            }
    
            // Move controller scroll view when tapping menu item
            let duration: Double = 0
    
            UIView.animateWithDuration(duration, animations: { () -> Void in
                let xOffset : CGFloat = CGFloat(index) * self.controllerScrollView.frame.width
                self.controllerScrollView.setContentOffset(CGPoint(x: xOffset, y: self.controllerScrollView.contentOffset.y), animated: false)
            })
        }
    }
    

    and than:

    
            // Initialize scroll menu
            self.pageMenu = CAPSPageMenu(viewControllers: self.controllers, frame: frame, pageMenuOptions: parameters)
    
            // Optional delegate
            pageMenu!.delegate = self
            pageMenu!.setStartIndexToPage(startIndex)
    
    点赞 评论 复制链接分享
  • weixin_39757893 weixin_39757893 4月前

    Here is little fix

    Change the existing moveToPage method to this: I just add new parameter named animated

    
    - (void)moveToPage:(NSInteger)index withAnimated:(BOOL)animated {
    
    .
    .
    .
    .
    
     if (animated) {
                [UIView animateWithDuration:duration animations:^{
                    CGFloat xOffset = (CGFloat)index * self.controllerScrollView.frame.size.width;
                    [self.controllerScrollView setContentOffset:CGPointMake(xOffset, self.controllerScrollView.contentOffset.y) animated:NO];
                }];
            } else {
                CGFloat xOffset = (CGFloat)index * self.controllerScrollView.frame.size.width;
                [self.controllerScrollView setContentOffset:CGPointMake(xOffset, self.controllerScrollView.contentOffset.y) animated:NO];
            }
    }
    

    Than add new method:

    
    - (void)moveToPage:(NSInteger)index {
        [self moveToPage:index withAnimated:YES];
    }
    

    When you want to set start index, just call moveToPage method with animated:NO.

    点赞 评论 复制链接分享
  • weixin_39711867 weixin_39711867 4月前

    +1

    点赞 评论 复制链接分享
  • weixin_39792803 weixin_39792803 4月前

    Convert to ObjC version:

    
    -(void)setStartIndexToPage:(int)index
    {
        if (index >= 0 && index < _controllerArray.count) {
            if (index!=_currentPageIndex) {
                _startingPageForScroll = index;
                _lastPageIndex = _currentPageIndex;
                _currentPageIndex = index;
                _didTapMenuItemToScroll = true;
    
                int smallerIndex = _lastPageIndex < _currentPageIndex ? _lastPageIndex : _currentPageIndex;
                int largerIndex = _lastPageIndex > _currentPageIndex ? _lastPageIndex : _currentPageIndex;
    
                if (smallerIndex + 1 != largerIndex) {
                    for (int i = smallerIndex + 1; i < largerIndex; i++ ) {
                        if (![_pagesAddedSet containsObject:@(index)]) {
    
                            [self addPageAtIndex:index];
    
                            [_pagesAddedSet addObject:@(index)];
                        }
                    }
                }
    
                [self addPageAtIndex:index];
                [_pagesAddedSet addObject:@(_lastPageIndex)];
    
    
            }
    
            double duration = 0;
    
            [UIView animateWithDuration:duration animations:^{
                CGFloat xOffset = (CGFloat)index * _controllerScrollView.frame.size.width;
                [_controllerScrollView setContentOffset:CGPointMake(xOffset, _controllerScrollView.contentOffset.y)];
            }];
    
        }
    }
    
    点赞 评论 复制链接分享
  • weixin_39615402 weixin_39615402 4月前

    It is working in iPhone 6 or 7 but, when I scroll view in iPhone 7 Plus or X is set to left alignment side scroll means x is -20 around

    点赞 评论 复制链接分享
  • weixin_39984661 weixin_39984661 4月前

    +1

    点赞 评论 复制链接分享
  • weixin_39989443 weixin_39989443 4月前

    +1

    点赞 评论 复制链接分享
  • weixin_39785524 weixin_39785524 4月前

    +1

    点赞 评论 复制链接分享

相关推荐