UISwipeGestureRecognizer for swift on xcode



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

 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
            }
        }
    }
}

我该怎么做才能使滑动手势工作?

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

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

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

请设置您的imageview的用户界面true,默认为false。

myCell.postedImage.userInteractionEnabled = true;

最新更新