我目前通过以下方式将图像设置到我的UIImageView中:
art_image.image = UIImage(named:(artworkPin.title!))
其中art_image是图像视图, artworkPin.title 是指图像的名称。但是,我想在UIImageView中添加第二个图像,如果它存在,我想把它编程为
art_image.image = UIImage(named:(artworkPin.title + "1")?)
这会有用吗?在这种情况下,我会将第二张图像命名为第一张图像的名称,但是使用' 1'最后。示例:' Photo.jpeg'和' Photo1.jpeg'如果存在Photo1.jpeg,那么两者都会出现在图片视图中。
感谢您的帮助。
答案 0 :(得分:0)
你一次只能在imageivew中显示1张图片,所以如果你的行如下:
art_image.image = UIImage(named:(artworkPin.title!))
art_image.image = UIImage(named:(artworkPin.title! + "1")?)
art_image只包含Photo1,前提是存在这样的图像,而且打开的artworkPin.title也不是nil,否则你会看到一些不同的结果。
如果您确实想要为图像视图添加多个图像以用于动画,则需要使用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()
如果您希望用户滑动,则需要进行一些更改,例如: 使用scrollview,例如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等
答案 1 :(得分:0)
我曾经遇到过类似的任务。我做的是,我使用<div id="cont">
</div>
<div id="pointer">
</div>
<button id='start'>start</button>
<button id='stop'>stop</button>
的框架创建了一个UIScrollView
,其UIImageView
将是contentSize
。
imageView.frame.size.width * numberOfImages
如果需要,您可以使用let scrollView = UIScrollView(frame: view.bounds)
view.addSubview(scrollView)
scrollView.contentSize = CGSize(width: scrollView.bounds.size.width * CGFloat(numberOfImages), height: scrollView.bounds.size.height))
for i in 0...<numberOfImages.count-1 {
let imageView = UIImageView(frame: CGRect(x: scrollView.bounds.size.width * CGFloat(i), y: scrollView.bounds.origin.y, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height))
imageView.image = UIImage(named: "artwork" + "\(i)")
scrollView.addSubview(imageView)
}
为其设置动画。
Timer