【问题标题】:Scrollview not scrolling in right滚动视图不向右滚动
【发布时间】:2020-10-05 12:18:02
【问题描述】:

我有一个滚动视图,我想以水平滚动方式显示用户选择的图像。以下是我的代码。但是,我无法做到这一点。请指导我。

var xPosition: CGFloat = 0
var scrollViewContentWidth: CGFloat = 398

func handlePickedImage(image: UIImage){
    let imageView = UIImageView(image: image)
    imageView.frame = CGRect(x: 0, y: 0, width: 398, height: 188)
    imageView.contentMode = UIView.ContentMode.scaleAspectFit
    imageView.frame.origin.x = xPosition
    imageView.frame.origin.y = 10


    let spacer: CGFloat = 10
    xPosition = 398 + spacer
    scrollViewContentWidth = scrollViewContentWidth + spacer
    imageScrollView.contentSize = CGSize(width: scrollViewContentWidth, height: 188)
    imageScrollView.addSubview(imageView)
}

【问题讨论】:

    标签: ios swift uiscrollview


    【解决方案1】:

    我用这段代码创建了这个,它还实现了分页:

    import UIKit
    
    class ImagePagerViewController: UIViewController {
        @IBOutlet weak var collectionView: UICollectionView!
    
        var data: [FoodPostImageObject] = []
        var userId: Int?
        var indexPath: IndexPath?
    
        var page: Int = 1
        var alreadyFetchingData = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
            collectionView.delegate = self
            collectionView.dataSource = self
    
            let layout = UICollectionViewFlowLayout()
            layout.itemSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
    
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            layout.scrollDirection = .horizontal
    
            collectionView.collectionViewLayout = layout
            if userId != nil {
                getUserImages()
            }
        }
        override func viewDidLayoutSubviews() {
            guard self.indexPath != nil else {return}
            self.collectionView.scrollToItem(at: self.indexPath!, at: .right, animated: false)
        }
    }
    
    extension ImagePagerViewController: UICollectionViewDataSource, UICollectionViewDelegate {
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImagePagerCell", for: indexPath) as! ImagePagerCollectionViewCell
            cell.image.loadImageUsingUrlString(urlString: data[indexPath.row].food_photo)
            return cell
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return data.count
        }
    }
    
    extension ImagePagerViewController {
        func fetchMoreData(){
            if !alreadyFetchingData {
                if userId != nil {
                    getUserImages()
                }
            }
        }
        func getUserImages(){
            guard let userId = self.userId else {return}
            alreadyFetchingData = true
            Server.get("/user_images/\(userId)/\(page)/"){ data, response, error in
                self.alreadyFetchingData = false
                guard let data = data else {return}
                do {
                    self.data.append(contentsOf: try JSONDecoder().decode([FoodPostImageObject].self, from: data))
                    DispatchQueue.main.async {
                        self.collectionView.reloadData()
                        self.collectionView.scrollToItem(at: self.indexPath!, at: .right, animated: false)
                        self.page += 1
                    }
                } catch {}
            }
        }
    }
    

    还有这个 UICollectionViewCell:

    import UIKit
    
    class ImagePagerCollectionViewCell: UICollectionViewCell {
        @IBOutlet weak var image: CellImageView!
    
    }
    

    在故事板中,我只有一个 collectionView 和 ImagePagerCollectionViewCell。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      相关资源
      最近更新 更多