How can I achieve Tinder effect in Swift?
I mean, I have an image and want to accept if I swipe to right and reject if I swipe to left.
I can do it with the code bellow:
@IBAction func SwipeRight(sender: UISwipeGestureRecognizer) {
UIView.animateWithDuration(1) {
self.Imagem.center = CGPointMake(self.Imagem.center.x - 150, self.Imagem.center.y )
}
//other things after acception
}
and
@IBAction func SwipeLeft(sender: UISwipeGestureRecognizer) {
UIView.animateWithDuration(1) {
self.Imagem.center = CGPointMake(self.Imagem.center.x + 150, self.Imagem.center.y )
}
//other things after rejection
}
But this way the user can't cancel the action. I want that if the user swipes to a delta distance from the edge (left or right), it would appear an image to let the user now that, if he ends the movement, the action will take place. Otherwise, the user can, without ending the movement, go back to a distance bigger than delta, and the action would be cancelled.
A TapGesture ends when the specified number of taps happen on the view it’s attached to. The following SwiftUI sample adds a TapGesture on a Circle and changes the color and shadow once the gesture is finished: .fill (didTap ?
Create a new Swift file and name it CardView.swift. Open the newly created CardView.swift file and add the following code: Nothing here is groundbreaking UI, but let’s break it down. Use geometry reader to size our content within the frame of its parent view. The variable geometry will allow us to access our parent's width and height.
The SwipeObserver class is responsible for updating the states based on the gesture interactions. The EnvironmentObject is instantiated in the SceneDelegate class in the following way: The following code tracks the drag state of each of the cards and updates the SwiftUI body accordingly:
I would like to thanks the people who suggested solutions. Follow the solution I developed with a huge help of a lot of people from Stack Overflow:
@IBAction func Arrastei(sender: UIPanGestureRecognizer) {
var origem = CGPoint(x: 0, y: 0)
var translation : CGPoint = sender.translationInView(Imagem)
var txy : CGAffineTransform = CGAffineTransformMakeTranslation(translation.x, -abs(translation.x) / 15)
var rot : CGAffineTransform = CGAffineTransformMakeRotation(-translation.x / 1500)
var t : CGAffineTransform = CGAffineTransformConcat(rot, txy);
Imagem.transform = t
if (translation.x > 100) {
LbResultado.textColor = btVerdadeiro.textColor
LbResultado.text = btVerdadeiro.text
LbResultado.hidden = false
} else {
if (translation.x < -100) {
LbResultado.textColor = btFalso.textColor
LbResultado.text = btFalso.text
LbResultado.hidden = false
} else {
LbResultado.hidden = true
}
}
if sender.state == UIGestureRecognizerState.Ended {
if (translation.x > 100) {
objJogo.Rodada_Vigente!.Responder(true)
} else {
if (translation.x < -100) {
objJogo.Rodada_Vigente!.Responder(false)
} else {
sender.view.transform = CGAffineTransformMakeTranslation(origem.x, origem.y)
sender.view.transform = CGAffineTransformMakeRotation(0)
}
}
}
}
This solution uses:
Imagem --> UIImageView
- to be accepted or rejected
LbResultado --> UITextView
- to show the user he is in acceptance or rejection area
There is no math calculations to set rotation and translation. I used values that give me a visually good effect.
The action (acceptance and rejection) area is when the user drag image more than 100 pixels to left (reject) or right (accept). If the user ends the movement out the action area, the image will go back to its original position.
I will be glad if someone suggests improvements to this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With