UIScrollView启用了分页刷卡问题

时间:2017-08-18 23:13:15

标签: ios swift uiscrollview

我有一个启用了分页的UIScrollView。一切正常,但有时当我滚动到下一个图像时,仍会显示最后一个图像的1个像素。

enter image description here

正如您在图像的最左侧所看到的,有一条像素垂直线来自它之前的图像。这意味着图像没有完全滑动。

我尝试了很多,但无法找到问题。

这是我的代码:

import Foundation
import UIKit
import SwiftyGif

class MBSPagingScrollView: UIScrollView, UIScrollViewDelegate {

var scrollView = UIScrollView()
var pageControl = UIPageControl()
var imagesArray = [String]()

override init (frame : CGRect) {
    super.init(frame : frame)
}

convenience init () {
    self.init(frame:CGRect.zero)
}

required init(coder aDecoder: NSCoder) {
    fatalError("This class does not support NSCoding")
}

init(frame : CGRect, imagesarray : [String]){
    super.init(frame : frame)

    imagesArray = imagesarray

    scrollView.delegate = self
    scrollView.isPagingEnabled = true
    scrollView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
    scrollView.showsHorizontalScrollIndicator = false
    self.addSubview(scrollView)

    pageControl.frame = CGRect(x: 0, y: self.frame.size.height - 50, width: self.frame.size.width, height: 50)
    pageControl.numberOfPages = imagesArray.count
    pageControl.currentPage = 0
    pageControl.tintColor = UIColor.red
    pageControl.pageIndicatorTintColor = UIColor.black
    pageControl.currentPageIndicatorTintColor = MainOrangeColor
    self.addSubview(pageControl)

    scrollView.contentSize = CGSize(width:scrollView.frame.size.width * CGFloat(imagesArray.count),height: scrollView.frame.size.height)
    pageControl.addTarget(self, action: #selector(changePage(sender:)), for: UIControlEvents.valueChanged)

    for (index, imageName) in imagesArray.enumerated() {

        var imageView = UIImageView()

        if imageName.hasSuffix(".gif") {
            let gifManager = SwiftyGifManager(memoryLimit:30)
            let gif = UIImage(gifName: imageName)
            imageView = UIImageView(gifImage: gif, manager: gifManager)
        }

        else {
            imageView = UIImageView(image: UIImage(named: imageName))
            imageView.contentMode = UIViewContentMode.scaleToFill
        }

        imageView.frame = CGRect(x: CGFloat(index) * self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        scrollView.addSubview(imageView)
    }

}

func changePage(sender: AnyObject) -> () {
    let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
    scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
    pageControl.currentPage = Int(pageNumber)
}

}

非常感谢任何帮助。 非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试为所有图片视图设置<button id="generate-btn">Generate Random Fact</button> <div id="random-fact-content"></div> <img id="random-fact-image"/>