I am stumped on how to get a certain behavior out of my experiment with UIPanGestureRecognizer.
When a user taps and holds an image, and it then intersects with another image, a behavior is triggered. In this case, the behavior is that the opacity of the green or red image goes to 100%.
Once a user continues dragging the image around the screen, and breaks the intersect with either the green or red image, I'd like its opacity to return to 50%. My control flow is getting ugly. I keep tweaking it, but am stumped to find the logic that gets me what I want.
@objc func handlePan(sender: UIPanGestureRecognizer) {
let imageOneView = sender.view!
let translation = sender.translation(in: view)
switch sender.state {
case .began, .changed:
imageOneView.center = CGPoint(x: imageOneView.center.x + translation.x, y: imageOne.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: view)
view.bringSubviewToFront(imageOne)
if imageOne.frame.intersects(winImage.frame) {
winImage.alpha = 1
} else {
if imageOne.frame.intersects(loseImage.frame) {
loseImage.alpha = 1
winImage.alpha = 0.5
} else {
return
}
}
case .ended:
if imageOne.frame.intersects(winImage.frame) {
UIView.animate(withDuration: 0.3, animations: {
self.imageOne.alpha = 0.0
})
performSegue(withIdentifier: "winnerDetailView", sender: self)
} else {
if imageOne.frame.intersects(loseImage.frame) {
UIView.animate(withDuration: 0.3, animations: {
self.imageOne.alpha = 0.0
})
navigationItem.rightBarButtonItem?.isEnabled = true
loseImage.alpha = 0.5
} else {
UIView.animate(withDuration: 0.3) {
self.imageOne.frame.origin = self.imageOneOrgin
}
}
}
default:
break
}
}
And additional code: https://github.com/ericmseitz/imageWar
Thanks.