Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicked button move to scrollView in swift

I have some question.

When i clicked button, how to move scroll View for next scroll page.

I made storyboard .

enter image description here

I have two direction button.

This is my code(This code not include two button)

import UIKit

class ScrollDetailVC: UIViewController, UIScrollViewDelegate {

let DetailScroll1 = ["image":"1"]
let DetailScroll2 = ["image":"2"]
let DetailScroll3 = ["image":"3"]

var DetailScrollArray = [Dictionary<String,String>]()

@IBOutlet weak var DetailScrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()
    DetailImageView()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func scrollViewDidScroll(_ scrollView: UIScrollView) {

        scrollView.contentOffset.y = 0

}

func DetailImageView() {

    DetailScrollArray = [DetailScroll1,DetailScroll2,DetailScroll3]
    DetailScrollView.isPagingEnabled = true
    DetailScrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(DetailScrollArray.count), height : self.view.bounds.height)
    DetailScrollView.showsHorizontalScrollIndicator = false
    DetailScrollView.showsVerticalScrollIndicator = false
    //firstScroll.alwaysBounceVertical = true

    DetailScrollView.delegate = self

    for (index,Scroll) in DetailScrollArray.enumerated() {

        if let scrollView = Bundle.main.loadNibNamed("Scroll", owner: self, options: nil)?.first as? scrollView {

            scrollView.Scrollimg.image = UIImage(named: Scroll["image"]!)

            scrollView.Scrollimg.contentMode = .scaleAspectFill  

            DetailScrollView.addSubview(scrollView)
            scrollView.frame.size.width = self.DetailScrollView.bounds.size.width
            scrollView.frame.size.height = self.DetailScrollView.bounds.size.height
            //scrollView.imageView?.contentMode = UIViewContentMode.scaleAspectFill
            scrollView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width

            //scrollView.contentMode = UIViewContentMode.scaleAspectFill
        }
    }
}

I want change scroll View image. Thanks.

like image 706
chen Avatar asked Feb 03 '26 07:02

chen


1 Answers

Change your scrollView's contentSize in your Button's Tap events (e.g. TouchUpInside).
Try the following tap handler.

leftButton.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside)  
rightButton.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside)  // add those two lines right after buttons' initialization.

func leftButtonTapped() {
     if DetailScrollView.contentOffset.x > 0 {
         DetailScrollView.contentOffset.x -=  self.view.bounds.width 
     }
}

func rightButtonTapped() {
         if DetailScrollView.contentOffset.x < self.view.bounds.width * CGFloat(DetailScrollArray.count-2) {
             DetailScrollView.contentOffset.x +=  self.view.bounds.width 
         }
    }
like image 72
JsW Avatar answered Feb 06 '26 00:02

JsW