How do I give users the ability to swipe through a

2019-07-20 13:45发布

I'm currently using a combination of UISwipeGestureRecognisers and an array of images but this isn't giving me the desired effect. When I swipe the image just appears but what I'm aiming for is a horizontal scrolling effect.

So for example if I partially scroll to the left, both the image I'm scrolling away from and the image I'm scrolling to will be on screen. I'm sure this can be achieved with a horizontal UIScrollView but not sure how to do this.

This is what I'm doing now.

My images are loaded from parse and stored in a imagesArray:

    query.findObjectsInBackgroundWithBlock { (objects: Array!, error: NSError!) -> Void in

        if error != nil {
            println("0 images found")
        } else {


            self.imagesArray.addObjectsFromArray(objects)

Set up the gesture recogniser in my viewDidload:

override func viewDidLoad() {
    super.viewDidLoad()
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: "didSwipe:")
    userImage.addGestureRecognizer(rightSwipe)

    var leftSwipe = UISwipeGestureRecognizer(target: self, action: "didSwipe:")

    leftSwipe.direction = UISwipeGestureRecognizerDirection.Left
    userImage.addGestureRecognizer(leftSwipe)
}

My didSwipe method:

func didSwipe(sender: UISwipeGestureRecognizer) {
    var direction = sender.direction as UISwipeGestureRecognizerDirection

    switch (direction) {
    case UISwipeGestureRecognizerDirection.Right:
        println("swiped right")

    case UISwipeGestureRecognizerDirection.Left:
        println("swiped left")
        println(imagesArray)
        userImage.file = imagesArray.objectAtIndex(1)["image"] as PFFile
        userImage.loadInBackground()
    default:
        println("ok")

    }


}

1条回答
乱世女痞
2楼-- · 2019-07-20 14:04

You want a UIScrollView. There is a great example and tutorial here.

查看更多
登录 后发表回答