Is there any way to detect when the user swipe across SCNNode?
I ve already tried UISwipeGestureReconizer but it didn't work for me. Any ideas?
Try adding a pan gesture recognizer
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGesture))
panRecognizer?.delegate = self
view.addGestureRecognizer(panRecognizer!)
Depending if you want to know which direction you'll have to check out the translation of the view.
#objc func panGesture(sender: UIPanGestureRecognizer){
let translation = sender.translation(in: sender.view)
print(translation.x, translation.y)
}
}
Make sure you have you add the delegate - UIGestureRecognizerDelegate
Hope that gives you a good start
Related
I have got an UITableView with a custom TableViewCell. I use a pan gesture for recognizing the positions while moving my finger to the left and to the right. On basis of the finger position I change some values in the labels in this TableViewCell. This works really great. But suddenly I can not scroll the TableView up and down. I already read the reason. Swift can not work with two gesture recognizers at the same time. And I found many examples of people how have nearly the same problem. I tried many of them but I can not fix my problem. I use Swift 5. Could you please describe a bit more precise how to fix my problem? Many thanks
import UIKit
class TVCLebensmittel: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.addGestureRecognizer(gestureRecognizer)
}
#IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began {
let translation = gestureRecognizer.translation(in: self)
// Put my finger on the screen
} else if gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self)
// While moving my finger ...
} else if gestureRecognizer.state == .ended {
let translation = gestureRecognizer.translation(in: self)
// Lift finger
}
}
...
}
The solution is to insert the pan gesture to the tableview and not to the tableviewcell. So I can listen to the left and right pan and also the up and down movement of the tableview.
I just share my approach. link It works very well. I needed custom swipe design while perform delete. Try it. If you need more information feel free to ask. Check it if you like.
This gestureRecognizerShouldBegin let you to use scroll while using UIPanGestureRecognizer.
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if (gestureRecognizer.isKind(of: UIPanGestureRecognizer.self)) {
let t = (gestureRecognizer as! UIPanGestureRecognizer).translation(in: contentView)
let verticalness = abs(t.y)
if (verticalness > 0) {
print("ignore vertical motion in the pan ...")
print("the event engine will >pass on the gesture< to the scroll view")
return false
}
}
return true
}
Does anyone know the best way how to implement this animation or library with similar functionality? I assume that it can be done via affine transforms. But maybe somebody knows some examples.
on tableView, you can use editActionForRowAt function(you can read more about it here https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614956-tableview) but because you are using a collection view you have to do it by yourself.
Add every cell a UIPanGestureRecognizer and make the animation according to the to the pan gesture.
like this:
func setupSwipeGesture() {
swipeGesture = UIPanGestureRecognizer(target: self, action:#selector(swiped(_:)))
swipeGesture.delegate = self
self.addGestureRecognizer(swipeGesture)
}
func swiped(_ gestureRecognizer: UIPanGestureRecognizer) {
let xDistance:CGFloat = gestureRecognizer.translation(in: self).x
// do your animation
}
I have a small view that I want to be moved around the screen and tapable. So I add a UIPanGestureRecognizer to move it, and a UITapGestureRecognizer to receive tap events like so:
let panner = UIPanGestureRecognizer(target: self, action: #selector(panDidFire(panner:)))
playerViewController.view.addGestureRecognizer(panner)
let tapper = UITapGestureRecognizer(target: self, action: #selector(viewTapped(tapper:)))
playerViewController.view.addGestureRecognizer(tapper)
And I create the actions in the same file
func viewTapped(tapper: UITapGestureRecognizer){
fadeInButtons()
}
func panDidFire(panner: UIPanGestureRecognizer) {
let offset = panner.translation(in: view)
panner.setTranslation(CGPoint.zero, in: view)
var center = playerViewController.view.center
center.x += offset.x
center.y += offset.y
playerViewController.view.center = center
}
The panDidFire() function is called when the user pans, but the viewTapped() function is not called at all.
Is there a trick to this? What am I doing wrong? Is there another way of going about this?
I am using Xcode 8 with swift 3
You have to set to your recognizers to work with other gesture recognizer on the view. Please use method of UIGestureRecognizerDelegate
gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
In my ViewController.swift, I have an array that contains custom UIViews. Every time one is created, I add a UIPanGestureRecognizer to it like this:
var panRecognizer = UIPanGestureRecognizer(target: self, action: "detectPan:")
newCard.gestureRecognizers = [panRecognizer]
This links to my detectPan(recognizer: UIPanGestureRecognizer) function, which handles movement. However, since I have multiple objects linked to the function, I'm not sure how I determine from which one the input is coming.
Is there anything like (the nonexistent) recognizer.target that I could use? Should I just handle the panning from within each of the custom UIViews instead?
Any help would be appreciated!
First of all, you should declare your panRecognizer with let.
let panRecognizer = UIPanGestureRecognizer(target: self, action: "detectPan:")
Second of all, you should not set the gestureRecognizers property of any UIView. This is bad practice because UIKit may have already added its own gesture recognizers to that view behind the scenes. If you subsequently remove those recognizers by assigning [panRecognizer] to that property, you may get unexpected behavior. To add your pan gesture recognizer, do this:
newCard.addGestureRecognizer(panRecognizer)
Then, in your detectPan(recognizer: UIPanGestureRecognizer) method you can detect which UIView was panned with the following code:
func detectPan(recognizer: UIPanGestureRecognizer) {
switch recognizer.view {
case self.customViewArray[0]:
// do something
case self.customViewArray[1]:
// do something else
case ... :
// ...
}
I'm making an iOS8 app using Swift. I'd like the user to be able to use gestures to reveal certain parts of the interface. So for example, the user slides their finger up and the view they slid their finger up moves out of the way, following their finger to reveal another view underneath.
What I'd like is a gesture to give a result similar to the notification box that you can pull down from the top of the screen. I've been looking at the documentation and I can't seem to find a suitable gesture.
I saw one called UISwipeGestureRecogniser, but the only problem is, it doesn't follow your finger, it simply runs a function when I slide my finger up / down.
Here's the documentation page:
https://developer.apple.com/documentation/uikit/uigesturerecognizer
You're looking for the UIPanGestureRecognizer. You'll find the Apple Documentation here.
Here's a sample handler that will move a view with your finger. In Interface Builder, add a UIPanGestureRecognizer to a view that you want to be able to drag. Set the delegate to your ViewController. Set the action to this action:
Swift 2.X:
#IBAction func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .Began || gestureRecognizer.state == .Changed {
let translation = gestureRecognizer.translationInView(self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x + translation.x, gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view)
}
}
Swift 3:
#IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
Of course, you can add the UIPanGestureRecognizer programmatically:
In viewDidLoad for your ViewController, create the recognizer and add it to the view you want to be able to drag:
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.someDraggableView.addGestureRecognizer(gestureRecognizer)