在图像数组中来回滑动 Swift

Swipe back and forth through array of images Swift

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

这是我的代码:

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

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

    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.


        }

    }


}

提前致谢!

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

var imageIndex: NSInteger = 0

那么你的刷卡功能应该是这样的

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.


    }

}


}

你可以做类似的东西。

首先您需要设置一个位置计数器,它会显示您目前所在的位置。重要提示:如果你想从第一个元素开始,你需要将 start 设置为 0,因为数组从 0:

开始计数
var swipePosition = 0

然后,如果向前滑动,需要检查当前swipePosition是否大于图像数量。为此,您可以使用数组的 count 方法。但是你必须减去 1,因为位置从 0 开始。所以如果它已经在你的数组中的最高或最低位置,你不需要做任何事情。否则你增加或减少一个位置:

//swipe forward
if swipePosition > imageList.count-1{
    //do nothing because it is already at the highest position
}else{
    swipePosition += 1
}

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

//swipe backward

if swipePosition == 0 {
    //Do nothing because it is already at start.
}else{
    swipePosition -= 1
}

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

`

@IBOutlet weak var img: UIImageView!    

var currentnImageIndex:NSInteger = 0

let arrayOfImages = ["Home Filled-50","Bullish-50","Line Chart Filled-50","Stack of Photos Filled-50","News-50","Download From Ftp Filled-50","Administrator Male Filled-50","Trophy Filled-50","Page Overview  Filled-50"]

override func viewDidLoad() {
    super.viewDidLoad()

    img.userInteractionEnabled = true//do not forget to right this line otherwise ...imageView's image will not move


    let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipedRight:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    img.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipedRight:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    img.addGestureRecognizer(swipeLeft)

    img.image = UIImage(named: arrayOfImages[currentnImageIndex])

    // Do any additional setup after loading the view.
}

func swipedRight(gesture : UIGestureRecognizer){

    if let swipeGesture = gesture as? UISwipeGestureRecognizer{
        switch swipeGesture.direction{
        case UISwipeGestureRecognizerDirection.Right:
            print("User swiped right")

            if currentnImageIndex < arrayOfImages.count - 1 {
            ++currentnImageIndex
            }

            if currentnImageIndex < arrayOfImages.count  {
            img.image = UIImage(named: arrayOfImages[currentnImageIndex])
            }
        case UISwipeGestureRecognizerDirection.Left:
            print("User swiped left")

           // --currentnImageIndex

            if currentnImageIndex > 0{
                --currentnImageIndex
            }
            if currentnImageIndex >= 0{
                img.image = UIImage(named: arrayOfImages[currentnImageIndex])
            }
           // if curretnImageIndex

        default:
            break
        }
     `
@IBOutlet weak var imageView: UIImageView!
    var imageIndex:NSInteger = 0
    var maximages = 3
    var imageList: [String] = ["burger", "burger2", "burger3", "burger4"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        self.view.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        self.view.addGestureRecognizer(swipeLeft)

        imageView.image = UIImage(named:"burger")
    }

    func swiped(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {

                case UISwipeGestureRecognizerDirection.right :
                    print("User swiped right")

                    // decrease index first

                    imageIndex -= 1

                    // check if index is in range

                    if imageIndex < 0 {

                        imageIndex = maximages
                    }

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

                case UISwipeGestureRecognizerDirection.left:
                    print("User swiped Left")

                    // increase index first

                    imageIndex += 1

                    // check if index is in range

                    if imageIndex > maximages {

                        imageIndex = 0
                    }

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

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