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
}
}
Related
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
view.bringSubviewToFront(tappedView)
is working when I drag (pan) views but not when I tap them. My issue is that when I have one view layered over the other, I want the bottom view to come to the front when tapped. Currently it will only come to the front when dragged. Do I need some additional code to do this? Thanks.
Here's an excerpt from my code for more context:
#objc func didPan(_ recognizer: UIPanGestureRecognizer) {
let location = recognizer.location(in: self.view)
switch recognizer.state {
case .began:
currentTappedView = moviePieceView.filter { pieceView -> Bool in
let convertedLocation = view.convert(location, to: pieceView)
return pieceView.point(inside: convertedLocation, with: nil)
}.first
currentTargetView = movieTargetView.filter { $0.pieceView == currentTappedView }.first
case .changed:
guard let tappedView = currentTappedView else { return }
let translation = recognizer.translation(in: self.view)
tappedView.center = CGPoint(x: tappedView.center.x + translation.x, y: tappedView.center.y + translation.y)
recognizer.setTranslation(.zero, in: view)
view.bringSubviewToFront(tappedView)
```
I had this same problem, I managed to solve it this way:
Add a gestureRecognizer to your view and put this in your code. Don't forget to set the delegate as well when attaching this to the code!
#IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
view.bringSubviewToFront(myView)
}
Put this in your viewDidLoad:
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
If you want to do that from your panGesture check my question.
Thank you all. It worked when I wrote a second function in addition to didPan().
This is the additional code:
override func viewDidLoad() {
super.viewDidLoad()
configureLabel()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPan(_:)))
view.addGestureRecognizer(panGestureRecognizer)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGestureRecognizer)
}
#objc func didTap(_ recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self.view)
currentTappedView = moviePieceView.filter { pieceView -> Bool in
let convertedLocation = view.convert(location, to: pieceView)
return pieceView.point(inside: convertedLocation, with: nil)
}.first
guard let tappedView = currentTappedView else { return }
view.bringSubviewToFront(tappedView)
}
HOWEVER, I found out you can also just tick the "User Interaction Enabled" box in the Image View if you don't want to do it programmatically.
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
})
}
}
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")
}
}