我有一些问题。
当我点击按钮时,如何移动滚动查看下一个滚动页面。
我制作了故事板。
我有两个方向按钮。
这是我的代码(此代码不包括两个按钮)
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
}
}
}
我想要更改滚动查看图像。 感谢。
答案 0 :(得分:1)
在按钮的点按事件中更改scrollView的contentSize(例如,TouchUpInside)。
尝试以下点击处理程序。
let tags = ["this", "is", "cool"]
let someTags: [String] = tags[1..<3].map { $0 } // ["is", "cool"]
答案 1 :(得分:0)
// Swift 5.0
func scrollLeft() {
if scrollView.contentOffset.x > self.view.bounds.width {
scrollView.contentOffset.x -= self.view.bounds.width
var frame: CGRect = self.scrollView.frame
frame.origin.x = scrollView.contentOffset.x
frame.origin.y = 0
self.scrollView.setContentOffset(CGPoint(x: frame.origin.x, y: frame.origin.y), animated: true)
}
}
func scrollRight() {
print(scrollView.contentOffset.x)
if scrollView.contentOffset.x < self.view.bounds.width * CGFloat(catItems.count - 1) {
scrollView.contentOffset.x += self.view.bounds.width
}
}