I've set up a tap gesture to complete an action like this:
func setupGestures() {
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTap.numberOfTapsRequired = 1
sceneView.addGestureRecognizer(singleTap)
}
#objc func handleSingleTap(_ sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizerState.ended {
jumpUp()
}
}
func jumpUp() {
if let action = jumpAction {
addBlocks()
playerNode.runAction(action)
}
}
However, I can tap multiple times and my object stays in the air (continues the jump action) as it's registering the tap gesture continuously rather than letting it finish first before another tap can be registered. How can I fix this so that a tap gesture can only happen one at a time? Other answers suggested adding the sender.state == UIGestureRecognizerState.ended but it hasn't worked.
Fixed it by passing sender as an argument to the jumpUp() function and adding sender.isEnabled = true in its the completion handler:
#objc func handleSingleTap(_ sender: UITapGestureRecognizer) {
sender.isEnabled = false
jumpUp(sender)
}
func jumpUp(_ sender: UITapGestureRecognizer) {
if let action = jumpAction {
addBlocks()
playerNode.runAction(action, completionHandler: {
sender.isEnabled = true
})
}
}
Related
UITapGestureRecogniser began state is not recognised, only ended is recognised.
override func viewDidLoad() {
let tapgr = UITapGestureRecognizer(target: self, action: #selector(tapTrigger(recongizer:)))
bottomBar.addGestureRecognizer(tapgr)
}
#objc func tapTrigger(recongizer: UITapGestureRecognizer){
if recongizer.state == .began{
print("recognised") // does not print
}else if recogniser.state == .ended{
print("ended") //prints
}
}
What I am trying to do is highlight a view(not the view the recogniser is added) when a touch is recognised and unhighlight when it is cancelled.
Instead of using UITapGestureRecognizer you can use UILongPressGestureRecognizer to get the different state.
Like this:
let tapgr = UILongPressGestureRecognizer(target: self, action: #selector(tapTrigger(recongizer:)))
tapgr.minimumPressDuration = 0
bottomBar.addGestureRecognizer(tapgr)
#objc func tapTrigger(recongizer: UITapGestureRecognizer){
if recongizer.state == .began{
print("recognised")
} else if recongizer.state == .ended{
print("ended") //prints
}
}
I'm trying to get a few gesture recognisers to work on a UIView. The UIview is in this case a retrieved SVG image, the library that I use is SwiftSVG.
But the actual added image is a UIView, so I think that is not the problem?
override func viewDidLoad() {
super.viewDidLoad()
let svgURL = URL(string: "https://openclipart.org/download/181651/manhammock.svg")!
let hammock = UIView(SVGURL: svgURL) { (svgLayer) in
svgLayer.fillColor = UIColor(red:0.8, green:0.16, blue:0.32, alpha:1.00).cgColor
svgLayer.resizeToFit(self.v2imageview.bounds)
}
hammock.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
hammock.isUserInteractionEnabled = true;
hammock.addGestureRecognizer(tap)
self.view.addSubview(hammock)
}
// function which is triggered when handleTap is called
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
How can I make the recognizer work?
Thanks
You need to set a frame
hammock.frame = ///
or constraints
The following UILongPressGestureRecognizer code works, however it repeatedly runs the long() press function when the button is held. I'd like to have it only run one time while the user is pressing. Is this possible?
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
tapGesture.numberOfTapsRequired = 1
shiftBtn.addGestureRecognizer(tapGesture)
shiftBtn.addGestureRecognizer(longGesture)
}
#objc func tap() {
print("Short Tap")
}
#objc func long() {
print("Long press")
}
Thanks!
You should call the long When gesture activated. it's called one time whenever long gesture active.
#objc func hitlongprass(_ sender : Any){
guard let longPress = sender as? UILongPressGestureRecognizer else
{ return }
if longPress.state == .began { // When gesture activated
long()
}
else if longPress.state == .changed { // gesture Calls multiple times by time changed
}
else if longPress.state == .ended { // When gesture end
}
}
func long() {
print("Long press")
}
Calling:
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(hitlongprass(_:)))
You can check state of gesture in long function. Ex check state is begin long press
Depends upon the PlaybackControl Visibity How to show and hide the Custom Subview added in contentOverlayView?
I want to do like Youtube TVOS app did.
I tried with UIPressesEvent but it is not giving me the exact touch events. It is giving me these:
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for item in presses {
switch item.type {
case .Menu:
self.customViews.alpha = 0
case .PlayPause:
self.player?.pause()
self.customViews.alpha = 0
default:
self.setVisibilityToPreviewView()
}
}
}
func setVisibilityToPreviewView () { //This wont work in all cases.
if self.previewView.alpha == 1 {
self.previewView.alpha = 0
} else {
self.previewView.alpha = 1
}
}
But with this Touch events i can only show and hide the subviews.
It should be hidden when the playbackcontrol is Hidden.
If I get the PlayBackControl Visibility values I don't need to worry about hiding these subviews.
Apple is Using AVNowPlayingPlaybackControlsViewController. It is not open for developers.
So I need to find some other better way to do this.
Please guide me how to do it.
You can register a tapGesture recognizer and then set its allowPressTypes property to UIPressType.Select, something like this
let tapRecognizer = UITapGestureRecognizer(target: self, action: "onSelect:")
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)];
self.view.addGestureRecognizer(tapRecognizer)
And inside your action button show or hide custom overlays.
Example: Add this code inside a view controller and on tap (selecting at empty area on front of remote, touch area) you will see a message on console.
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
self.view.addGestureRecognizer(tapGesture);
}
func onSelect(){
print("this is select gesture handler method");
}
Update: Below is the code which will create AVPlayerController and will register tapGestureRecognizer to playervc.view.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
playContent()
}
func playContent(){
let urlString = "<contentURL>"
guard let url = NSURL(string: urlString) else{
return
}
let avPlayer = AVPlayer(URL: url)
let playerVC = AVPlayerViewController()
playerVC.player = avPlayer
self.playerObj = playerVC
let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
self.view.addGestureRecognizer(tapGesture);
playerVC.view.addGestureRecognizer(tapGesture)
self.presentViewController(playerVC, animated: true, completion: nil);
}
Give a try to this, I think it should work fine.
I would like to make long press tableview Cells, But i'm getting error:
UIGestureRecognizer.Type' does not have a member named 'state'
Here's the code
override func viewDidLoad() {
super.viewDidLoad()
var gesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
gesture.minimumPressDuration = 2.0
self.view.addGestureRecognizer(gesture)
}
func longpressed() {
if(UIGestureRecognizer.state == UIGestureRecognizerState.Ended){
print("ended")
} else if (UIGestureRecognizer.state == UIGestureRecognizerState.Began){
print("began")
}
}
And Yes I've created Bridging-Header.h and imported this file:
#import <UIKit/UIGestureRecognizerSubclass.h>
I want swift tutorial not objective-c!
Add a gesture recogniser to your UITableView like
var gestureRec = UILongPressGestureRecognizer(target: self, action: "didTap:")
self.tableView.addGestureRecognizer(gestureRec)
Implement a didTap function, which would look something like this.
func didTap(sender : UIGestureRecognizer)
{
if sender.state == .Began {
var location = sender.locationInView(self.tableView)
var indexPath = self.tableView.indexPathForRowAtPoint(location)
var cell = self.tableView.cellForRowAtIndexPath(indexPath!)
}
}
This should work.
Try this in your longPress method:
func longpressed(sender: UILongPressGestureRecognizer) {
var state = sender.state
if(state.state == UIGestureRecognizerState.Ended){
print("ended")
} else if (state.state == UIGestureRecognizerState.Began){
print("began")
}
}