您一次只能在 imageivew 中显示 1 张图片,所以如果您有如下几行:
art_image.image = UIImage(named:(artworkPin.title!))
art_image.image = UIImage(named:(artworkPin.title! + "1")?)
art_image 将仅由 Photo1 组成,前提是存在这样的图像,并且未包装的 artifactsPin.title 也不为零,否则您可能会看到一些不同的结果。
如果您确实想将多个图像添加到图像视图以实现动画效果,则需要使用 UIImageView 的 animationImages 属性,该属性以 UIImages 数组为例
art_image.animationImages = [UIImage.init(named:"Photo")!,
UIImage.init(named:"Photo1")!]
希望对你有帮助
编辑
var imagesListArray = [UIImage]()
for position in 1...5
{
if let image = UIImage.init(named: ("Photo\(position)"))
{
imagesListArray.append(image)
}
}
art_image.animationImages = imagesListArray
art_image.animationDuration = 3.0
art_image.startAnimating()
这将是一种更安全的添加图像的方法,因此它只会添加不为零的图像并添加 Photo1、Photo2 ..... Photo5
关于您的其他问题:
如果你希望用户能够
你说的动画是什么意思?
我又添加了两行代码,结果如下:
art_image.animationDuration = 1.0
art_image.startAnimating()
它会给你这样的东西:
如果您希望用户滑动,则需要进行一些更改,例如:
例如使用滚动视图,collectionview 是最简单的,或者在滑动时使用手势识别器需要更改图像
编辑
看看这个例子。想象一下每个按钮都是您的注释,所以当我点击它时,图像会发生变化。
我有 3 个名为 Photo11.png、Photo21.png 和 Photo31.png 的图像,这是我在按钮处理程序中的代码
@IBAction func buttonTapped(_ sender: UIButton)
{
if let image = UIImage.init(named: sender.currentTitle!+"1")
{
art_image.image = image
}
}
如您所见,我正在设置带有按钮标题 +“1”的图像,以便显示 Photo11.png 或 Photo21.png 等