【问题标题】:UIView Swipe Gesture Changing imagesUIView 滑动手势改变图片
【发布时间】:2017-07-14 14:13:48
【问题描述】:

我正在尝试实现左右滑动手势来更改我的 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])
            }
        }
    }

当我尝试在任何注释上滑动时,我现在遇到了崩溃。

【问题讨论】:

    标签: ios swift uiview uiswipegesturerecognizer


    【解决方案1】:

    通常,您将拥有一个图像列表和一个索引值来跟踪您在列表中的位置。

    当您“向右滑动”时,索引会减 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 应用程序,这是一个特定的注释。这是唯一具有多个图像的注释,我目前让它根据单击的注释选择正确的图像。
    • 没有真正的区别...如果你得到这个注释 - 它有多个图像 - 而不是图像名称的硬编码列表,获取对每个图像的引用并将它们放在一个列表中。您可能需要以不同于 UIImage(named:) 的方式引用它们,但过程是相同的。
    • 我在上面添加了我如何尝试实现它的代码,但我产生了几个错误。
    • 嗯...嗯,其中一些是基础知识,所以如果您还不知道这将有多大帮助,但...我在这里编辑了您的 pastebin 代码:@ 987654321@ 我无法测试它,因为我没有显示地图/图钉等的其余应用程序,但这是一般的想法。
    • 我已将其更改为 if art_title.text == "Art Collection" 这不会导致任何错误,但是,当我尝试在任何注释上滑动时,它会使应用程序崩溃。我已经更新了我的代码文件。
    猜你喜欢
    • 2013-05-06
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多