0

hi fellow developer I want to swipe between uiview with scrollView while changing the the index and small indicator move left and right. but is not responding while I try to swipe. this is my picture and my setup to scroll between uiview. can you tell me where I am missing?

scroll view

    let segmentedControl    = UISegmentedControl()
    let containerView       = UIView()
    let indicatorView       = UIView()
    let leftView            = UIView()
    let rightView           = UIView()
    let scrollView          = UIScrollView()
    var slides              = [UIView]()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        slides.append(leftView)
        slides.append(rightView)
        segmentedControl.insertSegment(withTitle: "Harian", at: 0, animated: true)
        segmentedControl.insertSegment(withTitle: "Mata Pelajaran", at: 1, animated: true)

        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.addTarget(self, action: #selector(handleTap), for: .valueChanged)
        setupSlideScrollView()
    }

    func setupSlideScrollView() {
        view.addSubview(scrollView)
        scrollView.isPagingEnabled = true
        scrollView.translatesAutoresizingMaskIntoConstraints = false

        for i in 0..<slides.count {
            slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
            scrollView.addSubview(slides[i])
        }
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
        segmentedControl.selectedSegmentIndex = Int(pageIndex)
    }

    @objc func handleTap(_ sender: UISegmentedControl) {
        indicatorView.frame.origin.x = (segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)) * CGFloat(segmentedControl.selectedSegmentIndex)

        UIView.animate(withDuration: 0.5, animations: {
            self.view.layoutIfNeeded()
            self.indicatorView.transform = CGAffineTransform(scaleX: 0.7, y: 1)
        }) { (finish) in
            UIView.animate(withDuration: 0.4, animations: {
                self.indicatorView.transform = CGAffineTransform.identity
            })
        }
    }

  • instead of this you can use XLPagerTabStrip: https://github.com/xmartlabs/XLPagerTabStrip. with good handling still looking on your code – Siddhant Nigam Feb 14 '20 at 12:08
  • 1
    Are you setting delegate `scrollview.delegate = self`? – Kishan Bhatiya Feb 14 '20 at 12:14
  • I try to not use a library @SiddhantNigam pretty much, since when the developer who create that library will update it in the future and it will be pain in the ass while I have to follow it. that's why I prefer to make it myself. – Ferry Adi Wijayanto Feb 14 '20 at 12:15
  • @KishanBhatiya I forgot about that, and since you mention I already update it. but it still not scrolling – Ferry Adi Wijayanto Feb 14 '20 at 12:17
  • @FerryAdiWijayanto you have to set `contentSize` for `scrollView` equal as `segmentedControl`' frame size – Kishan Bhatiya Feb 14 '20 at 12:43
  • And put code in `scrollViewDidEndDragging ` method which is called when scrollView has completed scrolling – Kishan Bhatiya Feb 14 '20 at 12:57
  • okey I confuse haha, so what should I delete? the pageIndex in scrollViewDidScroll?. can you show me how? @KishanBhatiya – Ferry Adi Wijayanto Feb 14 '20 at 13:07
  • just copy code from `scrollViewDidScroll` and paste into `scrollViewDidEndDragging` and also set `contentSize` for `scrollView` – Kishan Bhatiya Feb 14 '20 at 13:45
  • no @KishanBhatiya I put this code in scrollViewDidEndDragging let pageIndex = round(scrollView.contentOffset.x / view.frame.width) scrollView.contentSize = segmentedControl.frame.size segmentedControl.selectedSegmentIndex = Int(pageIndex) – Ferry Adi Wijayanto Feb 15 '20 at 04:44
  • Maybe this helps: https://stackoverflow.com/questions/29074454/how-to-create-a-scroll-view-with-a-page-control-using-swift – Kishan Bhatiya Feb 15 '20 at 04:52

0 Answers0