I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS. I need to detect swipe up & down, and the callbacks for them.
Any help, appreciated. Thanks.
I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS. I need to detect swipe up & down, and the callbacks for them.
Any help, appreciated. Thanks.
You need two recognizers, one for swiping up, and the other for swiping down:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
and for the handler:
- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {
}
Finally, you add it to your view:
[view addGestureRecognizer:swipeUpGestureRecognizer];
The same for the other direction (just change all the Up
s to Down
s).
[UIApplication sharedApplication].delegate.window
; since you say you do not have a view, I guess the place where to put this code is your appDidFinishLaunching... the usual place would be a viewDidLoad
inside a view controller... –
Acquisitive [[CCDirector sharedDirector] view]
or self but that doesnt work either. –
Olid [[CCDirector sharedDirector].openGLView addGestureRecognizer:...
; This is what I happen to use and it works. –
Acquisitive [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeUpGestureRecognizer];
–
Olid This worked for me in Xcode 7.3. , Swift 2.2.
import UIKit
class Viewcontroller: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
createAndAddSwipeGesture()
}
private func createAndAddSwipeGesture()
{
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(Viewcontroller.handleSwipeLeft(_:)))
swipeGesture.direction = UISwipeGestureRecognizerDirection.Left
view.addGestureRecognizer(swipeGesture)
}
@IBAction func handleSwipeLeft(recognizer:UIGestureRecognizer)
{
print(" Handle swipe left...")
}
}
© 2022 - 2024 — McMap. All rights reserved.