gpt4 book ai didi

ios - ScrollView 不向右滚动

转载 作者:行者123 更新时间:2023-12-01 21:43:15 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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!

}

在 Storyboard 中,我只有一个 collectionView 和 ImagePagerCollectionViewCell 里面。

希望这可以帮助

关于ios - ScrollView 不向右滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62399191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com