如何从最后一个单元格滚动回滚动视图的第一个单元格以及如何在swift 4中为每个单元格添加一个按钮?

时间:2018-05-09 11:39:45

标签: timer uiscrollview uibutton swift4

我在我的项目中使用了滚动视图,它看起来像页面视图控制器,并且它使用计时器滚动并且运行良好。当到达最后一个单元格时,我希望滚动视图滚动到最后一个单元格的第一个单元格。 这是我用来更改单元格的代码

    @objc func moveToNextPage (){

    let pageWidth:CGFloat = self.scrollView.frame.width
    let maxWidth:CGFloat = pageWidth * 4
    let contentOffset:CGFloat = self.scrollView.contentOffset.x

    var slideToX = contentOffset + pageWidth

    if  contentOffset + pageWidth == maxWidth
    {
        slideToX = 0
    }
    self.scrollView.scrollRectToVisible(CGRect(x:slideToX, y:0, width:pageWidth, height:self.scrollView.frame.height), animated: true)
}
还有一件事! 我想为每个页面添加一个按钮但我不能这样做,所以这是我的滚动视图方法

func addscrollV()
{

    scrollView.delegate = self
    var selectingItem = Int()
    for i in 0...(initials?.response.count)! - 1 {
        if ((initials.initInstamall?.response[i].type!)!) == "0" {
            selectingItem = i
            break
        }
    }

    for image in 0...(initials?.response[selectingItem].itemLists.count)! - 1 {
        let imageView = UIImageView()
        let placeHolderImage = UIImage(named : "ic_logo")
        let url = "http://www.example/\((initials?.response[selectingItem].itemLists[image].imagePath!)!)"
        let urls = URL(string: url)
        imageView.kf.setImage(with: urls , placeholder : placeHolderImage ,options:[.transition(ImageTransition.fade(1.0))])
        let xCoordinate = self.contentView.frame.width * CGFloat(image)
        contentWidth += self.contentView.frame.width
        scrollView.addSubview(imageView)
        imageView.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)
        selectScrollImage.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)
        selectScrollImage.tag = image
        selectScrollImage.addTarget(self, action: #selector
            (scrollImageSelected), for: UIControlEvents.touchUpInside)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        scrollView.addSubview(selectScrollImage)
        imageView.addSubview(selectScrollImage)
        imageView.bringSubview(toFront: selectScrollImage)
        selectScrollImage.backgroundColor = UIColor.blue
    }

    scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}

0 个答案:

没有答案