单击按钮移动到 swift 中的 scrollView
Clicked button move to scrollView in swift
我有一些问题。
当我点击按钮时,如何将滚动视图移动到下一个滚动页面。
我制作了故事板。
enter image description here
我有两个方向键。
这是我的代码(此代码不包括两个按钮)
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
}
}
// 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
}
}
我有一些问题。
当我点击按钮时,如何将滚动视图移动到下一个滚动页面。
我制作了故事板。
enter image description here
我有两个方向键。
这是我的代码(此代码不包括两个按钮)
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
}
}
// 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
}
}