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
}
Related
I want to remove UIView from screen after user tap something except that view. (to visualize it for you I will upload sketch of my view)
And I want to remove blue UIView after user tap on something except buttons in this view. What should I use?
EDIT:
In blue UIView are two buttons and I want to remove that view when user tap on background image
I did what #yerpy told me to do but it isn't working.
func test(gestureRecognizer: UITapGestureRecognizer) {
print("test")
}
func setUpBackgroundImageView() {
self.view.addSubview(backgroundImageView)
backgroundImageView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
backgroundImageView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
backgroundImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
backgroundImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let tap = UITapGestureRecognizer(target: self, action: #selector(test(gestureRecognizer:)))
backgroundImageView.addGestureRecognizer(tap)
tap.delegate = self
}
And I also add shouldReceiveTouch function to UIGestureRecognizerDelegate. What am I doing wrong?
Add UIGestureRecognizer to the super view :
As you said you have image view as a background.
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped(gestureRecognizer:)))
imageView.addGestureRecognizer(tapRecognizer)
tapRecognizer.delegate = self
Adding target action :
func tapped(gestureRecognizer: UITapGestureRecognizer) {
// Remove the blue view.
}
And then inside UITapGestureRecognizerDelegate :
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view!.superview!.superclass! .isSubclass(of: UIButton.self) {
return false
}
return true
}
}
Hope it helps !
EDIT
Make sure that user can touch on the view by enabling : self.view.userInteractionEnabled = true
1- Add a view below your view, let call it overlay (gray one)
2- Add your container view with all your buttons inside (green one)
3- Add a tap gesture to the overlay (drag tap to overlay view)
4- Create a #IBAction of the tap to the viewcontroller
5- Write code to hide your green view inside the #IBAction
Image
Image
you can use UITapGestureRecognizer for that view
override func viewDidLoad() {
super.viewDidLoad()
// Add "tap" press gesture recognizer
let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
tap.numberOfTapsRequired = 1
backgroundimage.addGestureRecognizer(tap)
}
// called by gesture recognizer
func tapHandler(gesture: UITapGestureRecognizer) {
// handle touch down and touch up events separately
if gesture.state == .began {
} else if gesture.state == .ended {
}
}
You can
subclass with that background view, and implement an inherited method -beganTouches:(there is another para but I forget what it is)
Add a UITapGestureRecognizer to that view
Add a giant button that covers the entire screen under all the buttons. Anytime the user presses on the giant button underneath the smaller buttons, do what you want it to do.
Not the most elegant but it works :P
I have a UIScrollView. I put a tap gesture to it as this:
self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))
self.tapGesture.delegate = self
self.tapGesture.numberOfTapsRequired = 1
self.tapGesture.numberOfTouchesRequired = 1
self.tapGesture.cancelsTouchesInView = false
self.tapGesture.delaysTouchesBegan = false
self.tapGesture.delaysTouchesEnded = false
self.scrollView.addGestureRecognizer(self.tapGesture)
This works fine, except when the scrollview is scrolling (scrolling animation is happening, not user dragging), tap gesture is ignored.
How I am animating the scroll view:
UIView.animate(withDuration: 0.3, delay: 0.0,
options:[.beginFromCurrentState, .curveEaseInOut], animations:
{
self.scrollView.contentOffset = CGPoint(x:self.scrollView.contentOffset.x, y:yOffset)
}, completion: nil)
This scrollview is scrolling most the time and I am trying to get it to recognize the tap gesture while scroll view is animating scrolling ....
Take a look at the UIGestureRecogniserDelegate functions.
You should be able to specify that both the pan and tap gestures can be recognised at the same time with the following function:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return (gestureRecogniser is UIPanGestureRecogniser || gestureRecogniser is UITapGestureRecogniser) && (otherGestureRecognizer is UIPanGestureRecogniser || otherGestureRecognizer is UITapGestureRecogniser)
}
Note: Ensure your class conforms to the UIGestureRecogniserDelegate protocol and you set the gestures delegate so self.
This should work, but I am unable to fully test it right now.
UPDATE:
If you are trying to recognise a tap during an animation you will likely need to use the UIViewAnimationOptions.AllowUserInteraction option in the options of UIView.animateWithDuration. Used this other answer as a source
I want to override the default behavior of double tapping the mapView.
In my swift app I have a mapView in a static cell, so in the method cellForRowAt I've decided to add a UITapGestureRecognizer. This is how I do it:
func tableView(_ myTable: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath as NSIndexPath).row == 0 {
let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell
cell.mapView.isScrollEnabled = false //this works
//this does not:
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
cell.mapView.addGestureRecognizer(tap)
...
And then I have a simple function:
func doubleTapped() {
print("map tapped twice")
}
But when I tap twice the map - it zooms in and there's no print in the console log - so my code doesn't work. What did I miss?
You had to enforce that your own double tap gesture recognizer disables the standard double tap gesture recognizer of the mapView.
This can be done using a delegate method:
Declare your view controller as a delegate of a gesture recognizer, using UIGestureRecognizerDelegate
Define a property for your own double tap gesture recognizer:
var myDoubleTapGestureRecognizer: UITapGestureRecognizer?
Set up your double tap gesture recognizer, e.g. in viewDidLoad:
myDoubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
myDoubleTapGestureRecognizer!.numberOfTapsRequired = 2
myDoubleTapGestureRecognizer!.delegate = self
mapView.addGestureRecognizer(myDoubleTapGestureRecognizer!)
Note, that the delegate is set here.
Implement the following delegate method:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if ((gestureRecognizer == myDoubleTapGestureRecognizer) && (otherGestureRecognizer is UITapGestureRecognizer)) {
let otherTapGestureRecognizer = otherGestureRecognizer as! UITapGestureRecognizer
return otherTapGestureRecognizer.numberOfTapsRequired == 2
}
return true
}
So, when you double tap the mapView, this delegate method returns true if the other gesture recognizer is the built-in double tap recognizer of the mapView. This means that the built-in double tap recognizer can only fire if your own double tap recognizer fails to recognize a double tap, which it won’t.
I tested it: The map is no longer zoomed, and method doubleTapped is called.
Try using touchesBegan to identify the touch event, and you can call your custom handler when the event is trigerred
Add the mapview as a subview of a container view in the tableViewCell. Set constraints so that the mapview fills the entier container view. Disable the user interaction of the mapview and add the double tap gesture to the container view. This code will help.
let cell = myTable.dequeueReusableCell(withIdentifier: "cellStatic") as! MyTableDetailsCell
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
cell.mapView.isUserInteractionEnabled = false
cell.containerView.addGestureRecognizer(tap)
tap.numberOfTapsRequired = 2
Now the "doubleTapped" selector will be called when the mapview is tapped twice. All other user interactions including the rotation gesture of the mapview are disabled.
I am trying to determine if there is a means of programmatically setting a gesture recognizer state, to force it to begin prior to it actually detecting user input.
For example, I am adding a pan gesture recognizer to an image when a long press is detected, like so;
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
myImage.addGestureRecognizer(longPressRecognizer)
func longPressed(sender: UILongPressGestureRecognizer) {
let mainWidth = UIScreen.mainScreen().bounds.width
let mainHeight = UIScreen.mainScreen().bounds.height
let myView: UIView(frame: CGRect(x: 0, y: 0, width: mainWidth, height: mainHeight)
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
myView.addGestureRecognizer(gestureRecognizer)
self.view.addSubview(myView)
}
In the handlePan() function, I'm able to determine when the pan starts and ends;
func handlePan(gesture: UIPanGestureRecognizer) {
if gesture!.state == UIGestureRecognizerState.Began {
print("Started pan")
}
if gesture!.state == UIGestureRecognizerState.Ended {
print("Ended pan")
}
}
My issue is that, to detect when the gesture started, the user has to (1) long press on the image, (2) release their finger, (3) press and hold and start panning. Ideally, I'd like to have the user (1) long press on the image, (2) start panning.
To accomplish this, I'm imagining I need to figure out a way to "trick" things into believing that the pan gesture already began.
note: In practicality, there is more complexity than what's presented here, which is why I need to add a subview with the pan gesture, rather than just adding the pan gesture to the image directly.
What you want to do is add both gesture recognizes up front, set their delegates to your class, allow them to recognize simultaneously (using the below method), and only use the data from the pan when the long press has successfully been recognized.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
I would like to assign a unique behaviour for two-finger swipe on WKWebView.
Referring to this site, I wrote codes as below. It worked, but scrolling on webview got extremely slow.
Is there any better way to do it by avoiding slow scrolling?
let doubleSwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: "doubleSwiped2")
doubleSwipeGestureRecognizer.numberOfTouchesRequired = 2
doubleSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
self.webView!.addGestureRecognizer(doubleSwipeGestureRecognizer)
for gesture in self.webView!.scrollView.gestureRecognizers!{
let gestureClass = gesture.classForCoder
let gestureName = NSStringFromClass(gestureClass)
print(gestureName)
if gestureName.containsString("Swipe"){
// do nothing
} else {
gesture.requireGestureRecognizerToFail(doubleSwipeGestureRecognizer)
}
}
Perhaps this codes suits for your demand.
First, you set self to delegate of recognizer.
doubleSwipeGestureRecognizer.delegate = self
Second, you writes a method of UIGestureRecognizerDelegate in self class.
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if otherGestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer.numberOfTouches() == 2 {
return true
}
return false
}
Third, you delete 'for' sentence.
for gesture in self.webView!.scrollView.gestureRecognizers! {
…
}