我有一些问题。
当我点击按钮时,如何将 ScrollView 移动到下一个滚动页面。
我制作了 Storyboard。
我有两个方向按钮。
这是我的代码(此代码不包括两个按钮)
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
}
}
}
我想要更改滚动查看图像。 谢谢。
最佳答案
在 Button 的 Tap 事件(例如 TouchUpInside)中更改 scrollView 的 contentSize。
尝试以下点击处理程序。
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
}
}
关于ios - 单击按钮快速移动到 scrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49955019/