代码之家  ›  专栏  ›  技术社区  ›  RobertvdBerg

子视图上的页面控件

  •  1
  • RobertvdBerg  · 技术社区  · 11 年前

    我正在为教程屏幕创建带有子视图的视图。 Tutorial screen

    我希望使用页面控件可以滑动灰色子视图

    我已经实施了 http://www.appcoda.com/uipageviewcontroller-tutorial-intro/ 但这适用于整个ViewControllers,我希望它仅适用于子视图。。。

    有没有人知道如何使用滑动手势实现PageControl,以便只更改灰色子视图?有什么好的教程吗?

    2 回复  |  直到 11 年前
        1
  •  2
  •   Jasarien    11 年前

    灰色区域必须是 UIScrollView 其中内容视图包含教程的每个页面。设置 pagingEnabled YES 在您的滚动视图上,这样当您滚动时,它将捕捉到每个页面。

    然后需要使用 addTarget:action:forControlEvents: 并通过 UIControlEventValueChanged 作为事件。然后,动作必须是一种方法,根据页面控件的值是增加还是减少,告诉滚动视图向前或向后移动页面。您可以通过更改滚动视图的内容偏移量,或者让它滚动以显示特定的矩形来实现这一点。

    最后,实现 UI滚动视图 ,并使用指示滚动视图何时停止滚动的方法(您将需要同时执行末端减速、末端拖动和可能的末端滚动动画),并在滚动视图更改页面时更新页面控件的值。

    这就是它的全部内容。如果您需要更多详细信息,请阅读 UI滚动视图 , UIScrollViewDelegate UIPageControl .

        2
  •  2
  •   Ty Lertwichaiworawit    11 年前

    使用 UIView 而不是 ViewController

    这里有一个适合我的解决方案:

    1) 创建 scrollView pageControl 在NIB或故事板中

    2) 滚动视图 ViewDidLoad

        self.scrollView.backgroundColor = [UIColor clearColor];
        self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack; //Scroll bar style
        self.scrollView.showsHorizontalScrollIndicator = NO;
        //dont forget to set delegate in .h file
        [self.scrollView setDelegate:self];
    
        self.scrollView.showsVerticalScrollIndicator = YES; //Close vertical scroll bar
        self.scrollView.bounces = YES; //Cancel rebound effect
        self.scrollView.pagingEnabled = YES; //Flat screen
        self.scrollView.contentSize = CGSizeMake(640, 30);
    

    3) 创建您的 views 您需要在滚动视图中,并将它们添加到 Array

    //For instance, you want 3 views
        UIView *ViewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        [ViewOne setBackgroundColor:[UIColor redColor]];
    
        UIView *ViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width+1, self.scrollView.frame.size.height)];
        [ViewTwo setBackgroundColor:[UIColor greenColor]];
    
        UIView *ViewThree = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width+2, self.scrollView.frame.size.height)];
        [ViewThree setBackgroundColor:[UIColor blueColor]];
    
        //add all views to array
        NSMutableArray *viewsArray = [[NSMutableArray alloc] initWithObjects:ViewOne, ViewTwo, ViewThree, nil];
    

    4) pageControl在 查看DidLoad

        self.pageControl.numberOfPages = viewsArray.count;
        self.pageControl.currentPage = 0;
        self.pageControl.backgroundColor = [UIColor clearColor];
        [self.pageControl setTintColor:[UIColor whiteColor]];
    

    5) 全部加起来

        for(int i = 0; i < viewsArray.count; i++)
        {
            CGRect frame;
            frame.origin.x = (self.scrollView.frame.size.width *i) + 10;
            frame.origin.y = 0;
            frame.size = CGSizeMake(self.scrollView.frame.size.width - 20,     self.scrollView.frame.size.height);
    
            NSLog(@"array: %@", [viewsArray objectAtIndex:i]);
    
            UIView *view = [[UIView alloc] initWithFrame:frame];
            view = [viewsArray objectAtIndex:i];
            [self.scrollView addSubview:view];
    
    
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*viewsArray.count, self.scrollView.frame.size.height);
    }
    

    6) 跟踪滚动视图并更新页面控件 (不要忘记滚动视图代理)

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        CGFloat pageWidth = scrollView.frame.size.width;
    
        //int page = floor((scrollView.contentOffset.x - pageWidth*0.3) / pageWidth) + 1);
    
        self.pageControl.currentPage = (int)scrollView.contentOffset.x / (int)pageWidth;
        NSLog(@"CURRENT PAGE %d", self.pageControl.currentPage);
    }
    

    这应该会奏效。

    PS:对不起,这些神奇的数字。