DanielWolfgangTrebek
DanielWolfgangTrebek

Reputation: 81

UISwipeGestureRecognizer for swift on xcode

I've set up my Instagram like app and want to be able to swipe left and right on the images so that I can signify a like and dislike. Why is my code not working whenever I run it doesn't load and highlights

 myCell.postedImage.addGestureRecognizer(swipeRight)

Here is my code for the tableView

import UIKit
import Parse


class HomePage: UITableViewController {

var images = [UIImage]()
var titles = [String]()
var imageFile = [PFFile]()




   override func viewDidLoad() {
    super.viewDidLoad()

    println(PFUser.currentUser())

    var query = PFQuery(className:"Post")

    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {

            println("Successfully retrieved \(objects!.count) scores.")

            for object in objects! {

                self.titles.append(object["Title"] as! String)

                self.imageFile.append(object["imageFile"] as! PFFile)

                self.tableView.reloadData()

            }
        } else {
            // Log details of the failure
            println(error)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return titles.count


}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 500

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell

    myCell.rank.text = "21"
    myCell.votes.text = "4012"
    myCell.postDescription.text = titles[indexPath.row]

    imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

        if let downloadedImage = UIImage(data: data!) {

            myCell.postedImage.image = downloadedImage

        }
    }

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    myCell.postedImage.addGestureRecognizer(swipeRight)



    return myCell

}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                println("Swiped right")
                default:
                break
            }
        }
    }

}

What should I do to make the swipe gesture work?

Upvotes: 0

Views: 993

Answers (2)

MOHAMMAD ISHAQ
MOHAMMAD ISHAQ

Reputation: 998

Set your imageview's user interaction to true so that the gesture action perform on image

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
myCell.postedImage.userInteractionEnabled = true;
myCell.postedImage.addGestureRecognizer(swipeRight)

you can do this from xib if you are using custom cell.

Upvotes: 3

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Please set your imageview's user interation true which is bydefault false.

myCell.postedImage.userInteractionEnabled = true;

Upvotes: 0

Related Questions