Ios 单击按钮移动到swift中的滚动视图

Ios 单击按钮移动到swift中的滚动视图,ios,scrollview,swift4,Ios,Scrollview,Swift4,我有一些问题 当我点击按钮,如何移动下一个滚动页面的滚动视图 我制作了故事板 我有双向按钮 这是我的代码(此代码不包括两个按钮) 导入UIKit 类ScrollDetailVC:UIViewController、UIScrollViewDelegate{ 让DetailScroll1=[“图像”:“1”] 让DetailScroll2=[“图像”:“2”] 让DetailScroll3=[“图像”:“3”] var DetailScrollArray=[Dictionary]() @IBMOu

我有一些问题

当我点击按钮,如何移动下一个滚动页面的滚动视图

我制作了故事板

我有双向按钮

这是我的代码(此代码不包括两个按钮)

导入UIKit
类ScrollDetailVC:UIViewController、UIScrollViewDelegate{
让DetailScroll1=[“图像”:“1”]
让DetailScroll2=[“图像”:“2”]
让DetailScroll3=[“图像”:“3”]
var DetailScrollArray=[Dictionary]()
@IBMOutlet弱var详细滚动视图:UIScrollView!
重写func viewDidLoad(){
super.viewDidLoad()
DetailImageView()
//加载视图后执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func scrollViewDidScroll(scrollView:UIScrollView){
scrollView.contentOffset.y=0
}
func DetailImageView(){
DetailScrollArray=[DetailScroll1,DetailScroll2,DetailScroll3]
DetailScrollView.isPaginEnabled=true
DetailScrollView.contentSize=CGSize(宽度:self.view.bounds.width*CGFloat(DetailScrollArray.count),高度:self.view.bounds.height)
DetailScrollView.showsHorizontalScrollIndicator=false
DetailScrollView.showsVerticalScrollIndicator=false
//firstScroll.alwaysBounceVertical=true
DetailScrollView.delegate=self
对于DetailScrollArray.enumerated()中的(索引,滚动){
如果让scrollView=Bundle.main.loadNibNamed(“滚动”,所有者:self,选项:nil)?。首先作为scrollView{
scrollView.Scrollimg.image=UIImage(名称:Scroll[“image”]!)
scrollView.Scrollimg.contentMode=.ScaleSpectFill
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.ScaleSpectFill
scrollView.frame.origin.x=CGFloat(index)*self.view.bounds.size.width
//scrollView.contentMode=UIViewContentMode.ScaleSpectFill
}
}
}
我想更改滚动查看图像。
谢谢。

在按钮的点击事件中更改滚动视图的内容大小(例如触碰内部)。
请尝试以下tap处理程序

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 
         }
    }
leftButton.addTarget(self,action:#选择器(leftButtonTapped),for:.touchUpInside)
addTarget(self,action:#选择器(rightButtonTapped),for:.touchUpInside)//在按钮初始化之后添加这两行。
func leftButtonTapped(){
如果DetailScrollView.contentOffset.x>0{
DetailScrollView.contentOffset.x-=self.view.bounds.width
}
}
func rightButtonTapped(){
如果DetailScrollView.contentOffset.x
//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
    }
}
func scrollLeft(){
如果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),动画:true)
}
}
func scrollRight(){
打印(scrollView.contentOffset.x)
如果scrollView.contentOffset.x
谢谢。你的建议。但我做了一点修改。contentOffset是CGPoint,所以我设置了contentOffset.x。无论如何,谢谢!!!!@chen太好了,我帮了你,并对错误表示歉意:PThanks Hero@JsW。为我工作了不到5分钟。这是一次聚会,你是主宾。
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
    }
}