通过Swift图像来回来回滑动 [英] Swipe back and forth through array of images Swift

查看:117
本文介绍了通过Swift图像来回来回滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组图像,我希望能够向前(向左)滑动到下一个图像,或向后(向右)滑动到上一个图像。当imageList达到-1 /超出范围时,应用程序崩溃。我无法弄清楚如何将其保持在范围内的逻辑。

I have an array of images that I want to be able to swipe forward (left) to the next image, or back (right) to the previous image. When the imageList hits -1/out of range, the app crashes. I'm having trouble of figuring out the logic of how to keep it within range.

这是我的代码:

var imageList:[String] = ["image1.jpg", "image2.jpg", "image3.jpg"]
let maxImages = 2
var imageIndex: NSInteger = 1

滑动手势在我的viewDidLoad()方法中,不确定这是否正确...:

The swipe gestures are in my viewDidLoad() method, not sure if this is the right place... :

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeLeft = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

    image.image = UIImage(named:"image1.jpg")

}


func swiped(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {

        case UISwipeGestureRecognizerDirection.Right :
            println("User swiped right")

        /*No clue how to make it go back to the previous image and 
        when it hits the last image in the array, it goes back to 
        the first image.. */

        case UISwipeGestureRecognizerDirection.Left:
            println("User swiped Left")


            if imageIndex > maxImages {

                imageIndex = 0

            }

            image.image = UIImage(named: imageList[imageIndex])

            imageIndex++



        default:
            break //stops the code/codes nothing.


        }

    }


}

非常感谢!

推荐答案

首先,你的图像索引应设置为零,因为数组元素从零开始,但这不是问题的根源

First of all your image index should be set to zero since array elements starts from zero but thats not a source of your problem

var imageIndex: NSInteger = 0

然后你的刷卡功能应该如下:

then your swiped function should be like following

func swiped(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

    switch swipeGesture.direction {

    case UISwipeGestureRecognizerDirection.Right :
        println("User swiped right")

        // decrease index first

        imageIndex--

        // check if index is in range

        if imageIndex < 0 {

            imageIndex = maxImages

        }

       image.image = UIImage(named: imageList[imageIndex])

    case UISwipeGestureRecognizerDirection.Left:
        println("User swiped Left")

        // increase index first

        imageIndex++

        // check if index is in range

        if imageIndex > maxImages {

            imageIndex = 0

        }

       image.image = UIImage(named: imageList[imageIndex])




    default:
        break //stops the code/codes nothing.


    }

}


}

这篇关于通过Swift图像来回来回滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆