我正在制作一款类似于应用的Instagram,但我希望这些图片能够向左或向右滑动以获取相似内容。我正在使用xcode与swift和parse.this是我到目前为止,但由于某种原因我得不到代码的反应,它不适合我。如何让手势识别器为我工作,以便可以向左或向右滑动图像本身以接收或丢失一个点?
import UIKit
import Parse
class HomePage: UITableViewController {
var xFromCenter: CGFloat = 0
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)
}
}
var imageFile: UIImageView = UIImageView()
imageFile.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(imageFile)
var gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
imageFile.addGestureRecognizer(gesture)
imageFile.userInteractionEnabled = true
}
func wasDragged(gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
var label = gesture.view!
xFromCenter += translation.x
var scale = min(100 / abs(xFromCenter), 1)
label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)
gesture.setTranslation(CGPointZero, inView: self.view)
var rotation:CGAffineTransform = CGAffineTransformMakeRotation(xFromCenter / 200)
var stretch:CGAffineTransform = CGAffineTransformScale(rotation, scale, scale)
label.transform = stretch
if gesture.state == UIGestureRecognizerState.Ended {
if label.center.x < 100 {
println("Not Chosen")
} else if label.center.x > self.view.bounds.width - 100 {
println("Chosen")
}
}
}
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
myCell.postedImage.userInteractionEnabled = true
}
}
return myCell
}
}
如何进行滑动工作?