Reputation: 301
In my UiView
I have a subView
called 'container' which should perform a rotation animation either if the user swipe up or down.
Basically it's like a wheel where if the user swipe down the wheel swipe down of a certain degree while swiping up perform the opposite action.
Here's my viewDidLoad code:
override func viewDidLoad() {
super.viewDidLoad()
//Swipe Gesture Recognizers Setup
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeUp(_:)))
swipeUp.direction = .up
swipeUp.delegate = self as? UIGestureRecognizerDelegate
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeDown(_:)))
swipeDown.direction = .down
swipeDown.delegate = self as? UIGestureRecognizerDelegate
view.addGestureRecognizer(swipeDown)
view.addGestureRecognizer(swipeUp)
}
and these are the 2 functions for swipe up or down:
func swipeUp(_ sender: UISwipeGestureRecognizer) {
print("Swiped Up")
UIView.animate(withDuration: 1) {
self.borderContainer.transform = CGAffineTransform(rotationAngle: 0)
}
}
func swipeDown(_ sender: UISwipeGestureRecognizer) {
print("Swiped Down")
UIView.animate(withDuration: 1) {
self.borderContainer.transform = CGAffineTransform(rotationAngle: 45)
}}
Everything works great, consolle is printing the swipe direction everytime the action is performed BUT (yeah there's a BUT!) it calls the related function only ONCE in each direction. Meaning that if I swipe Down, then I can only Swipe up...
I want it to be able to keep swiping down or up anytime the user wants...
What am I doing wrong? I read about the UIGestureRecogniser rotating together with the view therefore it's not reachable anymore but I applied the solution to apply the Gesture Recogniser to the superView and not to the View I'm animating... Why does the Swipe Gesture Recognizer only work once?
Any help is highly appreciated!
Upvotes: 0
Views: 165
Reputation: 2149
CGAffineTransform choose only shortest path for rotation.Keep in mind that if you rotate something.Your swipe is ok but you need to modify rotation code a little bit to make it work.
Upvotes: 0
Reputation: 136
Take a close look at your swipe down function. You set the same value every time you swipe down.
You need to add 45 degrees to the current angle if you want to change it every time. Otherwise it will continue to be in the same position as before.
Try to use borderContainer.transform.rotated(by: 45)
.
Upvotes: 1