UIPageControl Works with UIScrollView

Asked 2 years ago, Updated 2 years ago, 47 views

I would like to link UIPageControl and UIScrollView installed on Storyboard with Xcode6+Swift.

import UIKit

classViewController:UIViewController, UIScrollViewDelegate {

    // Linking UIScrollView
    @IBOutlet weak var scrollView: UIScrollView!

    // Associating UIPageControl
    @IBOutlet weak var pageControl:UIPageControl!


    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }

}

I can only tie it up, and I can't write anything about what's important...
I would like to display the label and image along with the slide
If you are familiar with this, I would appreciate it if you could tell me how to work with UIPageControl and UIScrollView.

swift xcode

2022-09-29 22:01

2 Answers

The scroll position (contentOffset) and the selection page (currentPage) must be synchronized to work with UIScrollView and UIPageControl, so they must handle events.

For UIScrollView, implement scrollViewDidScroll of UIScrollViewDelegate in ViewController.

 funcscrollViewDidScroll(scrollview:UIScrollView){
    pageControl.currentPage=scrollView.contentOffset.x/scrollView.frame.size.width
}

UIPageControl picks up the tap

@IBAction funcpageControlTapped (sender:AnyObject) {
    scrollView.contentOffset=CGPointMake (scrollView.frame.size.width*pageControl.currentPage, 0)
}


2022-09-29 22:01

First of all, I will put the sample code.(After receiving your comment, I am writing to you.Both when you tap Page Control and when you scroll through the scroll view.)

import UIKit

classViewController:UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl:UIPageControl!

    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }

    @ IBAction function changePage(sender:UIPageControl){
        scrollView.setContentOffset (CGPoint (x:scrollView.bounds.size.width*CGFloat(sender.currentPage), y:0.0), animated:true)
    }

    US>funcscrollViewDidEndDeccelerating (scrollView:UIScrollView) {
        varpage=Int(scrollView.contentOffset.x/scrollView.bounds.size.width)
        pageControl.currentPage=page
    }

}

Page Control is a UIControl subclass, such as buttons."Connect" with Action, not as an Outlet.If you change the sender type from AnyObject to UIPageControl, it will be easier to write the code (without having to convert the sender).In the sample, the UIPageControl Action method is changePage().
I used the method scrollRectToVisible() to scroll through the scroll view, but I changed it because the code would be shorter using the method setContentOffset().
It is important to note that Swift cannot perform four different types of operations.Since CGFloat (floating point number) and Int (int) cannot be multiplied, we have converted one of them to CGFloat (value: Int).
The Delete method in Scroll View uses scrollViewDidEndDeccelerating() instead of scrollViewDidScroll().As you can see by using the former, when you tap Page Control, it blinks.This may be due to the deletion of not only user action but also programmed scrolling, but to avoid it.The former method moves in the middle of the scroll animation, but the latter method moves when the animation is complete.


2022-09-29 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.