Hello I'm planning a game and an essential part of the game is to move left and right by pressing the right side of the screen or the left side.
But how can I detect a long press?
Many Thanks! :)
It is not called long tap. A tap can't be long. It is called UILongPressGestureRecognizer. You can take a look at the documents here
if you want to do something while the user is holding down on the screen, you could use the touchesBegan and touchesEnded methods to detect the hold.
var touching = false
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = true
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
Related
I added the ability to close the keyboard by clicking on the space when the keyboard is opened in the Textfield, thanks to this code.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
customTextField.endEditing(true)
super.touchesBegan(touches, with: event)
}
But I want the user to be able to close the keyboard by swiping down as well.
We can give this feature to the table view keyboard as follows.
searchTableView.keyboardDismissMode = .onDrag
So is it possible to turn off a text field keyboard like this?
Can a UIPanGestureRecognizer be added while the user is touching and immediately start recognizing the pan gesture? I can only get it to work if I lift up my finger, touch again and start dragging.
From another answer, I've tried to subclass UIPanGestureRecognizer and override the touch event:
import UIKit.UIGestureRecognizerSubclass
class InstantPanGestureRecognizer: UIPanGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if (self.state == UIGestureRecognizer.State.began) { return }
super.touchesBegan(touches, with: event)
self.state = UIGestureRecognizer.State.began
}
}
This doesn't solve my problem though, probably because they already had their UIPanGestureRecognizer added, and I want to add mine while the user is already touching the screen and have it work at that point.
I'm switching between adding swipe and pan gestures depending on where the user the is interacting on an image. The require(tofail:UIGestureRecognizer) function hasn't worked for me.
I am creating an app in Swift and have all of the screen I need already mapped out in Png files. I simply want to import each image and then when the user clicks, just switch the image. I have never used Swift before and know that this is not traditional development, but how would I approach this to begin? Is there a way that I can link the saved data from NS storage to Healthkit?
So basically you want a simple controller that has an image has background and when the user tap on the view it changes the image ?
If this is what you want just link your imageView from the storyboard in your viewController then :
override func viewDidLoad(){
self.yourImageView.image = UIImage(named: "yourfirstimage.png")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.yourImageView.image = UIImage(named: "yournewimage.png")
}
I'm making a little game and I have come across a problem that I haven't been able to solve. The thing is that when I touch the screen an action occours. Well, I want that if there are 2 differents touches the action to happen 2 times, not only on the first one. Right now, if there are 2 players on the same device, the one who touches first is the one who wins, because the second player isn't able to even call the action. How can I invoke the action two times treating every touch recived as input?
In other words, I want to detect when there are two fingers on the screen and split every "finger" in the normal action for one finger.
Of course, my actions takes place here:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//code to get input info
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//calling actions with the arguments of touchesEnded and with the ones of touchesBegan
}
In your storyboard, you need to enable "Multiple Touch" for your view (it's in the Interaction section of the Attributes Inspector.) You can also set this property programmatically:
view.multipleTouchEnabled = true
However, if multiple touches happen at the same time, they will be passed in a single touchesBegan event. To make sure your app detects both touches, iterate over all of the touches in the set in touchesBegan:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
//code to get input info
}
}
I'm making a customkeyboard, it has almost 30 buttons.
And i'm using multi-touch event to check which button is being selected or touch over.
My way is detect current finger position and compare with button position.
But it seems too slow because i have to use for loop to check every single button coordinates.
Any faster way to check touchesmoved touches over 1 UIView or UIButton?
For your keyboard buttons, you need to override the UIView method touchesBegan and touchesEnded. Touches began will be called when a touch enters the buttons frame, and touches ended will be called when a touch exits the buttons frame. No comparing of coordinates needed.
class MyButton: UIButton {
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
//Button presses
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
//Button unpresses
}
}
Note that there is a method known as touchesMoved which can be overriden in any UIView. This function is called any time touches move. You don't necessarily need to check any time a touch moves within the button, only when a touch begins and exits a button, so you shouldn't use the touchesMoved method in this case.