Nice programing

UIScrollView에서 페이지 변경

nicepro 2020. 12. 27. 20:45
반응형

UIScrollView에서 페이지 변경


10 페이지의 UIScrollView가 있습니다. 나는 그들 사이를 넘길 수 있습니다. 또한 터치하면 이전 또는 다음 페이지로 이동하는 2 개의 버튼 (뒤로 버튼과 다음 버튼)을 갖고 싶습니다. 그래도 이렇게하는 방법을 알아낼 수없는 것 같습니다. 내 코드의 대부분은 Apple의 페이지 제어 샘플 코드에서 나옵니다. 아무도 도울 수 있습니까?

감사


버튼에 페이지 위치로 스크롤하도록 지시하기 만하면됩니다.

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);

다음은 Swift 4에 대한 구현입니다 .

func scrollToPage(page: Int, animated: Bool) {
    var frame: CGRect = self.scrollView.frame
    frame.origin.x = frame.size.width * CGFloat(page)
    frame.origin.y = 0
    self.scrollView.scrollRectToVisible(frame, animated: animated)
}

다음을 호출하여 쉽게 호출 할 수 있습니다.

self.scrollToPage(1, animated: true)

편집하다:

이를 수행하는 더 좋은 방법은 수평 및 수직 페이지 매김을 모두 지원하는 것입니다. 이를위한 편리한 확장은 다음과 같습니다.

extension UIScrollView {

    func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true) {
        var frame: CGRect = self.frame
        frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
        frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
        self.scrollRectToVisible(frame, animated: animated ?? true)
    }

}

이것은 UIScrollView에 확장을 생성하여 수직 또는 수평 페이지로 스크롤 할 수 있습니다.

self.scrollView.scrollTo(horizontalPage: 0)
self.scrollView.scrollTo(verticalPage: 2, animated: true)
self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)

scrollRectToVisible이 작동하지 않아 contentOffset에 애니메이션을 적용해야했습니다. 이 코드는 신속한 3에서 작동합니다.

func scrollToPage(_ page: Int) {
    UIView.animate(withDuration: 0.3) { 
        self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)
    }
}

들어 스위프트 3 이것은 내가 매우 편리 찾을 것을 확장은 다음과 같습니다

extension UIScrollView {
    func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval) {
        let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
        DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
            self.setContentOffset(offset, animated: animated)
        })
    }
}

그리고 이것을 다음과 같이 부릅니다.

scrollView.scrollToPage(index: 1, animated: true, after: 0.5)

다음과 같이 할 수 있습니다.

CGRect lastVisibleRect;
CGSize contentSize = [_scrollView contentSize];
lastVisibleRect.size.height = contentSize.height; 
lastVisibleRect.origin.y = 0.0; 
lastVisibleRect.size.width = PAGE_WIDTH; 
lastVisibleRect.origin.x  = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

[_scrollView scrollRectToVisible:lastVisibleRect animated:NO];

다음은 신속한 정적 메서드입니다.

static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) {
    var frame: CGRect = scrollView.frame
    frame.origin.x = frame.size.width * CGFloat(page);
    frame.origin.y = 0;
    scrollView.scrollRectToVisible(frame, animated: animated)
}

먼저 다음과 같은 UIScrollView 확장을 만듭니다.

extension UIScrollView {

    func setCurrentPage(position: Int) {
        var frame = self.frame;
        frame.origin.x = frame.size.width * CGFloat(position)
        frame.origin.y = 0
        scrollRectToVisible(frame, animated: true)
    }

}

그런 다음 전화하십시오.

self.scrollView.setCurrentPage(position: 2)  // where 2 is your desired page

scrollRectToVisible이 작동하지 않으면 다음을 시도하십시오.

let frame = scrollView.frame
    let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
    self.scrollView.setContentOffset(offset, animated: true)

신속한 솔루션

func changePage(pageControl: UIPageControl) {

    let page = CGFloat(pageControl.currentPage)
    var frame = self.scrollView.frame
    frame.origin.x = frame.size.width * page
    frame.origin.y = 0
    self.scrollView.scrollRectToVisible(frame, animated: true)
}

mjdth가 추가 한이 코드에 추가로 viewWillAppear 또는 viewDidAppear 에 배치해야합니다 .

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

참조 URL : https://stackoverflow.com/questions/1926810/change-page-on-uiscrollview

반응형