我正在构建一个类似于tinder的简单应用程序作为测试。它允许用户将照片(图像视图)左右拖动到左侧或右侧,然后新的一个将加载。但是当图像视图在点击左侧或右侧之前被释放时,它被发送到中间屏幕而不是其原始起始位置。我想我需要以某种方式存储图像视图的原始起始位置并稍后调用它但不确定如何操作
这是我的相关代码:
class SwipingViewController: UIViewController {
@IBOutlet var userImage: UIImageView!
@IBOutlet var infoLabel: UILabel!
var displayedUserId = ""
//set image position
func wasDragged(gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
let imageDrag = gesture.view!
imageDrag.center = CGPoint(x: self.view.bounds.width / 2 + translation.x, y: self.view.bounds.height / 2 + translation.y)
let xFromCenter = imageDrag.center.x - self.view.bounds.width / 2
let scale = min(100 / abs(xFromCenter), 1)
var rotation = CGAffineTransformMakeRotation(xFromCenter / 200)
var stretch = CGAffineTransformScale(rotation, scale, scale)
imageDrag.transform = stretch
if gesture.state == UIGestureRecognizerState.Ended {
var acceptedOrRejected = ""
if imageDrag.center.x < 100 {
acceptedOrRejected = "rejected"
print("not chosen" + displayedUserId)
} else if imageDrag.center.x > self.view.bounds.width - 100 {
acceptedOrRejected = "accepted"
print("Chosen")
}
if acceptedOrRejected != "" {
PFUser.currentUser()?.addUniqueObjectsFromArray([displayedUserId], forKey: acceptedOrRejected)
PFUser.currentUser()?.saveInBackgroundWithBlock({
(succeeded: Bool, error: NSError?) -> Void in
if succeeded {
} else {
print(error)
}
})
}
//Resets image position after it has been let go of
rotation = CGAffineTransformMakeRotation(0)
stretch = CGAffineTransformScale(rotation, 1, 1)
imageDrag.transform = stretch
imageDrag.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
updateImage()
}
}
以下是该问题的屏幕截图:
图片起始位置
图片结尾位置
在此先感谢您的帮助,我对swift来说还是一个新手