Pobieranie bieżącej strony

W widoku przewijania chcę wyświetlić bieżącą stronę, która jest wyświetlana(może strona nie jest poprawnym terminem). Nie mogę znaleźć żadnej zmiennej, która to trzyma. Ale myślę, że musi się gdzieś trzymać, ponieważ wskaźnik jest w stanie pokazać, który podgląd podglądu przewijania jest obecnie wyświetlany.

Czy jest to całkowicie ukryte przed nami, Czy Jest jakiś sposób, abym mógł się do niego dostać?

Author: TheNeil, 2010-11-09

14 answers

Nie ma właściwości UIScrollView dla bieżącej strony. Można ją obliczyć za pomocą:

int page = scrollView.contentOffset.x / scrollView.frame.size.width;

Jeśli chcesz zaokrąglić w górę lub w dół do najbliższej strony, użyj:

CGFloat width = scrollView.frame.size.width;
NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
 264
Author: AlexVogel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-04 02:56:55

Całkiem polecam użycie tego kodu

int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;

Ale jeśli używasz tego kodu, twój widok nie musi znajdować się dokładnie na stronie, którą daje Ci indexOfPage. Dlatego też zalecam użycie tego kodu tylko w tej metodzie

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{}

Który jest wywoływany, gdy twój scrollView kończy przewijanie i aby numer twojej strony był naprawdę ostry

Polecam ustawić scrollView na paged włączony tym kodem

[scrollView setPagingEnabled:YES];

Więc w końcu powinno tak wyglądać sposób

-(void) methodWhereYouSetYourScrollView
{
   //set scrollView
   [scrollView setPagingEnabled:YES];
   scrollView.delegate = self;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;
   //your stuff with index
}
 32
Author: Jiří Zahálka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-14 18:20:41

W swifcie zrobiłbym to w rozszerzeniu:

extension UIScrollView {
    var currentPage:Int{
        return Int((self.contentOffset.x+(0.5*self.frame.size.width))/self.frame.width)+1
    }
}

To po prostu zadzwoń:

scrollView.currentPage
 31
Author: Andrius Steponavičius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-16 13:35:59

Jak wyżej, ale jako kategoria


@interface UIScrollView (CurrentPage)
-(int) currentPage;
@end
@implementation UIScrollView (CurrentPage)
-(int) currentPage{
    CGFloat pageWidth = self.frame.size.width;
    return floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
@end
 12
Author: user363349,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-25 10:50:16

Inny sposób :

extension MyViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let width = scrollView.frame.width
        let page = Int(round(scrollView.contentOffset.x/width))
        print("CurrentPage:\(page)")
    }
}
 9
Author: Hemang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-03 12:36:47

Jest tu już kilka dobrych odpowiedzi. Jednak w przypadku scenariuszy, w których zawartość nie pasuje dokładnie do strony - i jeśli tak jak ja chcesz użyć wyniku dla UIPageControl, konieczne jest użycie funkcji ceil.

Weźmy przykład, w którym mam "cztery i trochę" stron treści.

Oprę to na moim obecnym przykładzie z prawdziwego życia. Szerokość rozmiaru zawartości wynosi 3140 punktów. W oparciu o rozmiar ramki mojej kolekcji, szerokość strony wynosi 728.

Tak Liczba stron równa się:

 3140 / 728 = 4.313 

Moje numberOfPages będzie pięć. Cztery z nich pokażą całość, a ostatnia - strona piąta - pokaże Pozostałe 0.313 treści.

Teraz, numberOfPages pięć oznacza, że indeksy stron będą 0, 1, 2, 3 i 4.

Kiedy przesuwam palcem w prawo, stroniąc w kierunku ostatecznego przesunięcia, metoda scrollViewDidEndDecelerating daje ostateczne przesunięcie X 2412.

Zastosowanie obliczenia zaokrąglenia:

2412 / 728 = 3.313 then rounded = 3

To nieprawda. użytkownik strony jest wyświetlanie według offsetu powinno być:

Offset / Page User Is Viewing
0        0
728      1
1456     2
2184     3
2412     4

Prawidłowe obliczenie za pomocą ceil:

private func pageForOffset(offset:CGFloat, pageWidth width:CGFloat) -> Int
{
    let page = Int(ceil(offset / width))
    NSLog("\(__FUNCTION__) offset \(offset) width \(width) page \(page) ")
    return page
}
 7
Author: Max MacLeod,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-20 12:03:42

Po prostu podziel bieżące przesunięcie przez rozmiar strony:

CGFloat pageNum = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
 6
Author: aoakenfo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-17 22:43:15

Rozwiązanie Aoakenfo jest niesamowite, jest to kolejne rozwiązanie łączące Twój kod z funkcją scrollViewDidScroll i PageController

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (sender == self.scroll) {
        int pageNum = (int)(self.scrPage.contentOffset.x / self.scrPage.frame.size.width);
        self.pagecontroller.currentPage =pageNum;
    }
}
 4
Author: pabloverd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-28 16:14:22

Dla Swifta użyłbym tego

    let width = scrollView.frame.width
    let page = round(scrollView.contentOffset.x/width)

Dość proste, w zasadzie zajmuje pozycję scrollview x, dzieli ją przez szerokość scrollview, a następnie zaokrągla.

 4
Author: neptunes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-11 04:53:54

W xCode 7.x swift 2.X można zrobić:

//MARK: - ScrollView Extensions
// Get the current page number
extension UIScrollView {
    var currentPage: Int {
        return Int(round(self.contentOffset.x / self.bounds.size.width))
    }

// If you have reversed offset (start from contentSize.width to 0)
    var reverseCurrentPage: Int {
        return Int(round((contentSize.width - self.contentOffset.x) / self.bounds.size.width))-1
    }
}
 3
Author: Alessandro Ornano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-19 13:56:34

Wyciągnąłem to z kilku naszych aplikacji...

- (NSInteger)currentPage
{
    if (0 == self.frame.size.width) {
        NSLog(@"frame width == 0!");
        return 0;
    }
    if (! self.currentPageValid) {
        _currentPage = round(self.contentOffset.x / self.frame.size.width);
        self.currentPageValid = YES;
    }
    return _currentPage;
}

Pełne źródło tutaj

 2
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-05 23:53:08

Czy używasz UIPageControl? Jeśli tak, to mA currentPage właściwość. Jeśli nie, myślę, że będziesz musiał obliczyć indeks strony z offsetu przewijania.

 1
Author: Rengers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-09 11:17:27

Jeśli ktoś szuka sposobu na zrobienie w C# dla Xamarin

    public int CurrentIndex
    {
        get => (int)Math.Round(this.scrollView.ContentOffset.X / this.scrollView.Frame.Width);
        set => this.scrollView.ScrollRectToVisible(new CGRect(new CGPoint(this.scrollView.Frame.Size.Width * value, 0), this.scrollView.Frame.Size), true);
    }

Ten getter i Setter powinny dostarczyć bieżącą stronę, a także umożliwić przewijanie do podanej strony

 0
Author: soan saini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-24 01:01:03

Swift 5.1 + rozszerzenie UIScrollView (pomijając słowo kluczowe return)

extension UIScrollView {

    var currentPage: Int {
        Int(round(self.contentOffset.x / self.frame.width))
    }
}
 0
Author: Diego Carrera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-22 14:56:39