UIView轻扫手势更改图像
问题描述:
我想实现左右轻扫手势来更改我的UIView上的图像。UIView轻扫手势更改图像
我也做了以下内容:
if art_title.text == "Art Collection" {
var imageList = ["Boulder Bean","Mother of Earth","Bamboozled","Black Figures","Modest Angel"]
var index = 0
func leftSwipe(_ sender: UISwipeGestureRecognizer) {
if index < imageList.count - 1 {
index = index + 1
art_image.image = UIImage(named: imageList[index])
}
}
func rightSwipe(_ sender: UISwipeGestureRecognizer) {
if index > 0 {
index = index - 1
art_image.image = UIImage(named: imageList[index])
}
}
}
我现在得到一个崩溃当我尝试刷上的任何注解。这是崩溃日志:
很抱歉,如果这不是完全清楚,我是新来这个。让我知道你是否需要我澄清一些事情。我已发布我的完整代码Here
答
通常,您将拥有图像列表和索引值以跟踪列表中的位置。
当你“向右滑动,即可”你减1索引...你向左滑动时,您可以通过1
递增指数如果该指数低于0(你的数组的第一个元素),你可以将它重置为0并保持原样,或者“将它包裹”到数组的末尾。同样,如果索引大于数组中的元素数量,则可以“将其包围”为零。
因此,举例来说:
// initialize
var imageList = ["flower", "balloon", "cat", "dog"]
var index = 0
// set the first image
art_image.image = UIImage(named: imageList[index])
// on swipe left (arrays are Zero based)
if index < imageList.count - 1 {
index = index + 1
art_image.image = UIImage(named: imageList[index])
}
// on swipe right
if index > 0 {
index = index - 1
art_image.image = UIImage(named: imageList[index])
}
等等...
这应该让你对你的方式。
我不能做一个图像列表,因为这个应用程序是一个MapView应用程序,这是一个特定的注释。这是唯一具有多个图像的Annotation,我现在可以根据单击的Annotation来选择正确的图像。 –
没有真正的区别......如果你看到这个注解 - 它有多个图像 - 而不是一个硬编码的图像名称列表,请将每个图像的引用放到列表中。您可能需要以与UIImage(named :)不同的方式引用它们,但过程相同。 – DonMag
我已经添加了上面的代码我如何尝试实现它,但我产生了几个错误。 –