UIScrollView启用分页启用刷卡问题

问题描述:

我有启用分页的UIScrollView。所有的作品都很好,但有时当我滚动到下一张图像时,仍然会显示最后一张图像的1个像素。UIScrollView启用分页启用刷卡问题

enter image description here

,你可以在最左边的图像看,有1个像素的垂直线是从图像前。这意味着图像没有完全被擦掉。

我试了很多,但找不到问题。

这里是我的代码:

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) 
} 

} 

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

尝试并设置所有图像浏览的clipsToBounds = true

+0

你好,我试过了,但不幸的是它仍然是一样的 –