开发者

Simple Page Control

开发者 https://www.devze.com 2023-04-11 11:58 出处:网络
I have a scroll view which switches between two views.The two views are set up in viewDidLoad.I want to have a page control that switches when the according view is selected.

I have a scroll view which switches between two views. The two views are set up in viewDidLoad. I want to have a page control that switches when the according view is selected.

All tutorials I have looked at have been far to complex.

- (void)viewDidLoad {

    [super viewDidLoad];

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

    CGRect pageFrame = CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.bounds.size.height);
    scrollView.contentSize = CGSizeMake(pageFrame.size.width 开发者_StackOverflow* 2, pageFrame.size.height);
    scrollView.pagingEnabled = YES;

    view1.frame = pageFrame;
    [scrollView addSubview:view1];


    pageFrame.origin.x += pageFrame.size.width;
    view2.frame = pageFrame;
    [scrollView addSubview:view2];
}

- (IBAction)changePage:(id)sender {

}


When a user taps a page control to move to the next or previous page, the control sends the UIControlEventValueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.

Set the delegate of the page control for the ValueChanged action the changePage method of your controller.

- (IBAction)changePage:(id)sender {
   UIPageControl *pageControl = (UIPageControl *)sender;
   NSInteger currentPage = pageControl.currentPage;
   CGPoint offset = CGPointMake(currentPage * scrollView.frame.size.width, 0);
   [scrollView setContentOffset:offset animated:YES];
}


Set an int as the tag property of your UIButtons to track which one was pressed.

-(IBAction)changePage:(id)sender {
   UIButton *button = (UIButton *) sender;
   CGPoint offset = view1.frame.origin;
   if (button.tag == kConstantForSecondPage) {
      offset = view2.frame.origin;
   }
   [scrollView setContentOffset:offset animated:YES];
 }
0

精彩评论

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

关注公众号