sam
sam

Reputation: 461

UIPageControl --To go to specific dot?

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)?

Upvotes: 1

Views: 4654

Answers (6)

jordiz
jordiz

Reputation: 289

You can do with rx or protocol:


import UIKit
import RxSwift

public class SpecificDotPageControl: UIPageControl {
    
    //If you don't use rx, use a protocol here for detect that the currentPage is changed.
    public let currentPageChanged = PublishSubject<Int>()
    
    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            guard let subview = self.subviews.first else {
                return
            }
            let touchPoint = touch.location(in: subview)
            let dotSize = subview.bounds.size.width / CGFloat(numberOfPages)
            let jumpToPage = touchPoint.x / dotSize
            self.currentPage = Int(jumpToPage)
            self.currentPageChanged.onNext(currentPage)
        }
    }
}

Example to use:

pageControl
     .currentPageChanged
     .subscribe { [weak self] currentPage in
         //Here you can change your page or collectionView page.
     }.disposed(by: disposeBag)

Upvotes: 0

Rustam Khisamov
Rustam Khisamov

Reputation: 301

Swift version of Frade's answer:

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let touchPoint = touch.location(in: self)
        
        
        let dotSize = bounds.size.width / CGFloat(numberOfPages)
        let jumpToPage = touchPoint.x / dotSize;
        self.currentPage = Int(jumpToPage)
    }
}

Example of touchBegan function took from hackingwithswift

Upvotes: 0

Frade
Frade

Reputation: 2988

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];
}

Upvotes: 6

Alex Terente
Alex Terente

Reputation: 12036

  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

Upvotes: 1

Anton
Anton

Reputation: 2297

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.

Upvotes: 0

dasdom
dasdom

Reputation: 14073

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.

Upvotes: 0

Related Questions