UIPageControl --To go to specific dot?

2020-07-30 02:04发布

问题:

i have two questions,

1) If i have 10 dots (UIPageControl) and i want to directly go on any specific dot say 5 from from 1 without traveling through 2 to 4. How i'll do that?

2)& Is it possible to travel automatically through dots, like after 2 min view will change with dots without user interaction (like we see in advertisements)?

回答1:

  1. pageControl.currentPage = 5

  2. Yes you need to set an NSTimer.

    myTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeDot:) userInfo:nil repeats:YES];
    

Edit:

NSTimer Class Reference



回答2:

I know its been awhile. But I will give my solution, just for the record.

Using a UIPageControl, I was trying to jump to a page, by taping on a dot. To do that, in a UIPageControl subclass I add a touch handler:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

and by knowing the size of each dot, calculate the index of the dot that was taped -> page. As added the touch handler, the selector to UIControlEventValueChanged stoped working, so just call: - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents check it here

Nothing difficult, it ends up with something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    float dotSize = self.bounds.size.width / (self.numberOfPages + 2);
    int jumpToPage = (int)touchPoint.x / dotSize;
    self.currentPage = jumpToPage;
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}


回答3:

1) The underlying view is a scroll view, right? Then you can set the contentOffset in code.

2) You can create a scheduled timer which changes the contentOffset.



回答4:

For the first one, it is not possible (see the docs):

The page control advances only one page in either direction.

As for the second one, you should just set up a timer.



标签: iphone