I would like to get get the location of first touch when users starts panning from the bottom of the screen and only then read position and use it.
I know that I can check .begin state but don't know how to keep track of the pan if this condition is true. It seemed like very easy thing to do but I didn't manage to do it.
#objc private func handleSwipe(swipe: UIPanGestureRecognizer) {
if swipe.state == .began && swipe.location(in: self.view).y < self.view.frame.height * 0.15 {
// do something with swipe.translation(in: self.view).y
}
}
Just store the coordinates in a variable, and retrieve it when you handle the .changed state. All gesture recogniser callbacks are on the main thread, so you shouldn't have issues with maintaining your own state between these callbacks.
Related
I have a view controller in which a user can move around UIButton, UISlider, and a custom UIView based control by panning the control around the screen. This view controller is used to create custom layout of control by the user. This is all done by adding PanGestureRecognizer to the UIControl to move the position of the control relative to user's finger location.
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(pan))
panRecognizer.delegate = self;
myslider.addGestureRecognizer(panRecognizer)
~~~~~~~~~~~~
//pan handler method
#objc func pan(_ gestureRecognizer: UIPanGestureRecognizer) {
let translation = gestureRecognizer.translation(in: view)
guard let gestureView = gestureRecognizer.view else {
return
}
//Move the center to user's finger location
gestureView.center = CGPoint(x: gestureView.center.x + translation.x, y: gestureView.center.y + translation.y);
gestureRecognizer.setTranslation(.zero, in: view);
if (gestureRecognizer.state == .ended) {
//Save the new location inside data. Not relevant here.
}
}
This worked fine in iOS 13 and below for all the control i mentioned above (with the UISlider being a bit glitchy but it still responded to the pan gesture and i don't need the value of the uislider anyway so it's safe to ignore). However testing the app in iOS 14 reveals that UISlider completely ignore the PanGesture (proven by adding breakpoint that never got called inside the pan gesture handling method).
I have looked at apple's documentation regarding UISlider and found no change at all related to gesture handling so this must be done deliberately in deeper lever. My question is: is there any way to "force" my custom gesture to be executed instead of UISlider's gesture without the need to create transparent button overlay (which i don't know will work or not) / creating dummy slider just for this ?
Additionally i also added UILongPressGestureRecognizer and UITapGestureRecognizer to the control. Which worked fine on other UIButton but completely ignored by the UISlider in iOS14 (in iOS 13 everything worked fine).
Thanks.
Okay.. I found the answer myself after some more digging and getting cue from "claude31" about making a new clean project and testing from there.
The problem was with the overriden beginTracking function. This UISlider of mine is actually subclassed into a custom class and in there the beginTracking function is overriden, as per code below:
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let percent = Float(touch.location(in: self).x / bounds.size.width)
let delta = percent * (maximumValue - minimumValue)
let newValue = minimumValue + delta
self.setValue(newValue, animated: false)
super.sendActions(for: UIControl.Event.valueChanged)
return true
}
This is to make the slider move immediately to the user finger location if the touch is inside the boundaries of the UISlider (without the need to first touch the thumbTrack and sliding it to the position the user wants).
In iOS 13 this function does not block the gestureRecognizer from getting recognized. However in iOS 14B4, overriding this function, with sendActions(for:) method inside it cause the added gestureRecognizer to be ignored completely, be it pan gesture, long press gesture, or even tap gesture.
For me, the solution is to simply add a state to check whether the pan gesture is required in this view controller or not. Because, luckily, i only need the added gestureRecognizer in view controller that does not require the beginTouch function to be customized and vice versa.
Edit:
Originally i wrote that the cause of the problem is due to always true return value, however, I just reread the documentation and the default return value for this function is also true. So i think the root causes of this problem is the sendActions(for:) method, causing the added gestureRecognizer to be ignored. My Answer above has been edited to reflect this.
I'm having an issue with handling screen rotations when a UIPanGestureRecognizer is in the .changed state. My handling logic looks something like:
#objc fileprivate func handlePanGesture() {
let state = self.panGestureRecognizer.state
if state == .began {
// Log beginning state
} else if state == .changed {
// Track position, update constraints
} else if state == .ended {
// Reset and prepare for new gesture
}
}
Everything seems to be working well, but when the device is rotated to a new orientation (without lifting my finger), the gesture recognizer stops receiving updates for the active touch, but never triggers the handlePanGesture() with the .ended state set.
Right now I'm handling this by looking out for viewWillTransition(to:with:) and cleaning up the state when that happens, but this approach fails when, e.g., an iPad is rotated from portrait-right-side-up to portrait-upside-down--there is never any size transition. Is there something that can be done to cancel the gesture recognizer on rotation (and trigger a call to handlePanGesture())?
You also need to check for the .cancelled state.
btw, switch on the state rather than if, else if, else if, else if
so there are some similar questions on here, but they don't really answer the questions/problem I am having.
The following explanation of my project may or may not help, I am adding it here just in case...
I am trying to build my first iOS Game, and I have built a scene using the scene editor. The scene contains a SKNode named "World". There is then a backgroundNode and a foregroundNode that are children of the world node. Everything in the scene right now, all SKSpriteNodes, are children of the backgroundNode.
In my GameScene.swift file I have variables attached to the background and foreground nodes so that I can add children to these nodes as the game progresses.
Hopefully this is clear so far...
Now I have added 5 other *.sks files to my project. These file contain scenes that I have made that will be added as children of the foreground node through code in my GameScene.swift file. The SKSpriteNodes in these scene files are placed in the foreground, but their z-position is less than the z-position of one of the background child nodes. This is because I want to have a box appear behind a light beam (the light beam is apart of the background and the box is added to the foreground). Here is a picture in case I caused any confusion
My problem is this...
I want to tap on the screen using gesture recognizers so that when I tap on the box I can then do some stuff. Trouble is that since the light beam has a greater z-position (cause of the effect I want), every time I use the atPoint(_ p: CGPoint) -> SKNode method to determine what node I tapped on I get returned the light beam node and not the box node.
How do I tap on just the box? I have tried changing the isUserInteractionEnabled property for the lights to false already, and I have tried using touchesBegan as shown in many of the other responses to similar question. I have also tried reading the swift developer documents provided by Apple, but I can't figure this out.
The code for my gesture Recognizers is below:
//variables for gesture recognition
let tapGesture = UITapGestureRecognizer()
let swipeUpGesture = UISwipeGestureRecognizer()
//set up the tap gesture recognizer
tapGesture.addTarget(self, action: #selector(GameScene.tappedBox(_:)))
self.view!.addGestureRecognizer(tapGesture)
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
//handles the tap event on the screen
#objc func tappedBox(_ recognizer: UITapGestureRecognizer) {
//gets the location of the touch
let touchLocation = recognizer.location(in: recognizer.view)
if TESTING_TAP {
print("The touched location is: \(touchLocation)")
}
//enumerates through the scenes children to see if the box I am trying to tap on can be detected (It can be detected using this just don't know how to actually detect it by tapping on the box)
enumerateChildNodes(withName: "//Box1", using: { node, _ in
print("We have found a single box node")
})
//tells me what node is returned at the tapped location
let touchedNode = atPoint(touchLocation)
print("The node touched was: \(String(describing: touchedNode.name))")
//chooses which animation to run based on the game and player states
if gameState == .waitingForTap && playerState == .idle{
startRunning() //starts the running animation
unPauseAnimation()
}else if gameState == .waitingForTap && playerState == .running {
standStill() //makes the player stand still
unPauseAnimation()
}
}
Hopefully this is enough for you guys... If you need some more code from me, or need me to clarify anything please let me know
Please read notes in the code. You can get what you want easily in spriteKit. Hope you get the answer.
#objc func tappedBox(_ recognizer: UITapGestureRecognizer) {
let touchLocation = recognizer.location(in: recognizer.view)
// Before you check the position, you need to convert the location from view to Scene. That's the right location.
let point = (recognizer.view as! SKView).convert(touchLocation, to: self)
print ( getNodesatPoint(point, withName: "whatever Node name" ) )
}
// 2 : This function gives you all nodes with the name you assign. If you node has a unique name, you got it.
/// You can change name to other properties and find out.
private func getNodesatPoint(_ point : CGPoint , withName name: String) -> [SKNode] {
return self.nodes(at: point).filter{ $0.name == name}
}
You want to use nodesAtPoint to do a hit test and get all of your nodes that is associated with the point. Then filter the list to find the box you are looking for.
If you have a lot of layers going on, you also may want to just add an invisible layer on top of your graphics that handles nodes being touched
Your other option is to turn off isUserInteractionEnabled on everything except the boxes and then override the touch events for your boxes, but that would mean you can't use gestures like you are doing now.
I am currently working on a project in which I need the user to press on a node to make an action occur. So currently I am working with some code that will allow this to happen but only if the user touches the screen.
I want it to work when the user touches and holds a specific node. Any help is appreciated! My code is in a picture below.
Thank You!
Since you already know how to do the gesture the rest is easy:
All we are going to do is take the point from the view, convert it to scene coordinates, and grab 1/all node(s) from the scene.
#IBAction func TELE(_ gestureRecognizer : UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began{
var touchPoint = gestureRecognizer.location(in: view)
var touchPointInScene = view.scene.convertPoint(fromView:touchPoint)
//use atPoint for the deepest node, node(:at) for all nodes)
var node = view.scene.atPoint(touchPointInScene)
var nodes = view.scene.nodes(at:touchPointInScene)
}
}
I'm trying to adjust the position of my textField as I long-press my view, but for some reason the while-loop never stops running. My code looks like this:
func buttonLongPressed(gestureRecognizer:UIGestureRecognizer){
if textEdit.editing == true{
self.textEdit.endEditing(true)
}
while gestureRecognizer.state == UIGestureRecognizerState.Began{
println("BEGAN")
self.textEdit.frame = CGRectMake(0, gestureRecognizer.locationInView(self.view).y, self.view.frame.width, 44)
}
}
I don't understand why this shouldn't work, and how to do it in any other way.
Any suggestions would be appreciated.
The gesture recognizer calls your action method when its state changes--it's not valid to poll the state from a while loop, it will never change.
It probably works like this:
Events are sent from the touch screen with a signal to wake up your app.
The gesture recognizer looks at the queued events and decides to enter "Began" state.
The gesture recognizer code invokes your action method (buttonLongPressed)
Your code enters a while look and reads gestureRecognizer.state repeatedly.
You can see, if your action method never returns, the gesture recognizer will never wake up again and look at new input.
You can probably just change your function like this:
func longPressAction( g:UILongPressGestureRecognizer )
{
switch g.state
{
case: .Changed
{
// handle one drag update... but don't loop
}
}
}