gpt4 book ai didi

ios - 如何在 iOS swift 中水平滚动图像

转载 作者:行者123 更新时间:2023-11-28 09:51:00 25 4
gpt4 key购买 nike

我有一个名为“cardImgView”的 ImageView ,因为我想通过水平滚动来加载两个图像,我尝试了以下方法,在这种情况下我只能上下滚动并且图像也不会改变,任何人 知道如何正确地做到这一点。

let img: UIImage = self.dataDict.object(forKey: kCardImgFront) as! UIImage
let img2:UIImage = self.dataDict.object(forKey: kCardImgBack) as! UIImage
imgArray = [img, img2]

for i in 0..<imgArray.count{
cardImgView?.image = imgArray[i]
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(cardImgView!)
}

提前致谢。

最佳答案

首先,正如我评论的那样,您当前使用的是单个 UIImageView --- 所以每次通过您的 for 循环时,您只是替换它的 .image一个 ImageView 。

其次,使用自动布局和约束会更好,而不是尝试显式设置框架和 scrollView 的 contentSize。

第三,UIStackView 非常适合您的用例 - 添加您想要水平滚动的多个图像。

所以,总体思路是:

  • 添加 ScrollView
  • 将堆栈 View 添加到 ScrollView
  • 使用约束使堆栈 View 控制 ScrollView 的内容大小
  • 为每张图片创建一个新的UIImageView
  • 将每个 ImageView 添加到堆栈 View

这是一个简单的示例,您可以在 Playground 页面中运行该示例以了解其工作原理。如果您将自己的名为 image1.png 和 image2.png 的图像添加到 playground 的资源中,它们将被使用(否则,此示例将创建纯蓝色和纯绿色图像):

import UIKit
import PlaygroundSupport

// UIImage extension to create a new, solid-color image
public extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
}

class TestViewController : UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// create a UIScrollView
let scrollView = UIScrollView()

// we will set the auto-layout constraints
scrollView.translatesAutoresizingMaskIntoConstraints = false

// set background color so we can see the scrollView when the images are scrolled
scrollView.backgroundColor = .orange

// add the scrollView to the view
view.addSubview(scrollView)

// pin scrollView 20-pts from top/bottom/leading/trailing
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20.0).isActive = true

// create an array of empty images in case this is run without
// valid images in the resources
var imgArray = [UIImage(color: .blue), UIImage(color: .green)]

// if these images exist, load them and replace the blank images in imgArray
if let img1: UIImage = UIImage(named: "image1"),
let img2: UIImage = UIImage(named: "image2") {

imgArray = [img1, img2]

}

// create a UIStackView
let stackView = UIStackView()

// we can use the default stackView properties
// but can change axis, alignment, distribution, spacing, etc if desired

// we will set the auto-layout constraints
stackView.translatesAutoresizingMaskIntoConstraints = false

// add the stackView to the scrollView
scrollView.addSubview(stackView)

// with auto-layout, scroll views use the content's constraints to
// determine the contentSize,
// so pin the stackView to top/bottom/leading/trailing of the scrollView
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0.0).isActive = true
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0.0).isActive = true
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0).isActive = true
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0).isActive = true

// loop through the images
for img in imgArray {

// create a new UIImageView
let imgView = UIImageView(image: img)

// we will set the auto-layout constraints, and allow the stackView
// to handle the placement
imgView.translatesAutoresizingMaskIntoConstraints = false

// set image scaling as desired
imgView.contentMode = .scaleToFill

// add the image view to the stackView
stackView.addArrangedSubview(imgView)

// set imgView's width and height to the scrollView's width and height
imgView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, multiplier: 1.0).isActive = true
imgView.heightAnchor.constraint(equalTo: scrollView.heightAnchor, multiplier: 1.0).isActive = true

}

}

}

let vc = TestViewController()
vc.view.backgroundColor = .red
PlaygroundPage.current.liveView = vc

关于ios - 如何在 iOS swift 中水平滚动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48075798/

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