xcode swift 的 UISwipeGestureRecognizer

UISwipeGestureRecognizer for swift on xcode

我已经设置了我的 Instagram 类应用程序,并希望能够在图像上左右滑动,以便表示喜欢和不喜欢。为什么我的代码在我 运行 不加载和突出显示时不起作用

 myCell.postedImage.addGestureRecognizer(swipeRight)

这是我的 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
            }
        }
    }

}

我应该怎么做才能使滑动手势起作用?

请设置您的图像视图用户交互 true,默认情况下为 false。

myCell.postedImage.userInteractionEnabled = true;

将您的 imageview 的用户交互设置为 true,以便对图像执行手势操作

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

如果您使用的是自定义单元格,则可以从 xib 执行此操作。