多个子视图被添加到UIScrollview

时间:2018-02-15 17:58:32

标签: swift uiscrollview

在我的用户个人资料页面上,有一个滚动视图用户滑过以查看更多照片。一切都适用于添加照片,但问题是添加到scrollviews子视图的每个图像都会添加两次。这是我用来填充scrollview的代码。我目前在故事板上的scrollview中嵌入了Imageview,如果用户没有照片,则将其用作默认值。底部应显示错误。我已经尝试使用clipstobounds,scaleaspectfit和设置背景颜色的组合来清除但没有停止在背景中显示其他图像。任何想法,将不胜感激。另外,在iphone 8 plus

上只会注意到这个bug
 var pages: Int = 1
    var numPhotos: CGFloat = 1
    var position: CGFloat = 1
    let scrollWidth: CGFloat = profileImage.frame.width
    let scrollHeight: CGFloat = profileImage.frame.height
    let scrollY: CGFloat = profileImage.frame.origin.y

 if let profileImg = user.profileImage {
        profileResource = ImageResource(downloadURL: URL(string: profileImg)!, cacheKey: profileImg)
        profileImage.kf.setImage(with: profileResource)
    } else {
        profileImage.image = #imageLiteral(resourceName: "requests_icon")
    }

    nameLbl.text = user.fullName
    influenceLbl.text = "\(user.score)"
    followersLbl.text = "\(user.followers)"
    followingLbl.text = "\(user.following)"

    if let image1 = user.photo1 {
        let imageOne = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
        imageOne.contentMode = .scaleAspectFit
        photo1Resource = ImageResource(downloadURL: URL(string: image1)!, cacheKey: image1)
        imageOne.kf.setImage(with: photo1Resource)
        pages = pages + 1
        numPhotos = numPhotos + 1
        position = position + 1
        scrollView.addSubview(imageOne)
    } else {

    }

    if let image2 = user.photo2 {
        let imageTwo = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
        imageTwo.contentMode = .scaleAspectFit
        photo2Resource = ImageResource(downloadURL: URL(string: image2)!, cacheKey: image2)
        imageTwo.kf.setImage(with: photo2Resource)
        pages = pages + 1
        numPhotos = numPhotos + 1
        position = position + 1
        scrollView.addSubview(imageTwo)
    } else {

    }

    if let image3 = user.photo3 {
        let imageThree = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
        imageThree.contentMode = .scaleAspectFit
        photo3Resource = ImageResource(downloadURL: URL(string: image3)!, cacheKey: image3)
        imageThree.kf.setImage(with: photo3Resource)
        pages = pages + 1
        numPhotos = numPhotos + 1
        position = position + 1
        scrollView.addSubview(imageThree)
    } else {

    }

    scrollView.contentSize = CGSize(width: profileImage.frame.width * numPhotos, height: scrollHeight)
    scrollView.delegate = self
    scrollView.contentMode = .scaleAspectFit
    scrollView.clipsToBounds = true

    pageControl.numberOfPages = pages
    pageControl.currentPage = 0
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
    let pageWidth:CGFloat = scrollView.frame.width
    let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth) + 1

    self.pageControl.currentPage = Int(currentPage)
}

enter image description here

0 个答案:

没有答案