Display menu list on Double Tap gesture - Swift3 - swift

I'm new to IOS and I'm trying to display list of buttons on double tap of UIView. I have the below code to capture the double tap gesture:
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.minimumPressDuration = 0.0
menuTap.numberOfTapsRequired = 2
menuTap.delaysTouchesBegan = true
self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
}
I need to design a panel to show a list of buttons such as start, stop and pause. Can anybody guide me on how to design a panel on the tapped position?

You need to use UITapGestureRecognizer for that not the UILongPressGestureRecognizer. Set UITapGestureRecognizer with numberOfTapsRequired to 2.
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.numberOfTapsRequired = 2
//No need to set isUserInteractionEnabled to true because by default it is true for `UIView`
//self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
Add action method of tapGesture like this.
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
let point = recognizer.location(in: self.view)
//Get your view from nib
let view = CustomView()
//set its origin to this point
view.frame.origin = point
//add your view in self.view
self.view.addSubview(view)
}

Related

How to highlight one part of a tableview cell in swift?

I have two clicks of gesture recognizer in my tableview cell to access different storyboards.
My second click is in the extreme right of my cell and I want when I click on it to just highlight that button and not the whole cell which includes the other button
So my question is : how to separate highlights of different clicks in a tableview cell ?
First you need to set the
tableView.allowsSelection = false
You can add two views in the cell and add tap
func setAccessoryViewTapGestures() {
var tapGestureforLeftAccesoryView = UITapGestureRecognizer()
tapGestureforLeftAccesoryView = UITapGestureRecognizer(target: self, action: #selector(leftAccessoryViewTapped(_:)))
tapGestureforLeftAccesoryView.numberOfTapsRequired = 1
tapGestureforLeftAccesoryView.numberOfTouchesRequired = 1
leftAccessoryView.addGestureRecognizer(tapGestureforLeftAccesoryView)
leftAccessoryView.isUserInteractionEnabled = true
var tapGestureforRightAccesoryView = UITapGestureRecognizer()
tapGestureforRightAccesoryView = UITapGestureRecognizer(target: self, action: #selector(rightAccessoryViewTapped(_:)))
tapGestureforRightAccesoryView.numberOfTapsRequired = 1
tapGestureforRightAccesoryView.numberOfTouchesRequired = 1
rightAccessoryView.addGestureRecognizer(tapGestureforRightAccesoryView)
rightAccessoryView.isUserInteractionEnabled = true
}
Here rightAccessoryView and leftAccessoryView are the view in the cell
#objc private func leftAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
#objc private func rightAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
I think this helps

Tap Gesture isn't working. User interaction enabled.

Have following view structure:
Programmatically adding tap gesture rec to Temp lbl:
let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
tempLblTap.delegate = self
tempLblTap.numberOfTapsRequired = 1
tempLblTap.numberOfTouchesRequired = 1
tempLblTap.cancelsTouchesInView = false
self.tempLbl.isUserInteractionEnabled = true
self.tempLbl.addGestureRecognizer(tempLblTap)
but the method convertDegrees isn't triggered.
There are also 2 swipe gesture recognizers that are added to the same view:
let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(leftSwipeGestureRecognizer)
let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Maybe they are the reason?
Found solution:
View that contained my lbl had its userInteractionEnabled unchecked in storyboards.
So, when adding gesture recognizers to labels, images or simply adding buttons, always check that all parent views has its userInteractionEnabled to true.

Why is my image not showing up Fullscreen?

I have the following problem. I have about 10 different images which are in a view controller. Now I want the image to switch to fullscreen when they are tapped on. I already set the allow user interaction option to yes, and put in the following code i found on this website:
import UIKit
class ImageViewController: UIViewController {
#IBAction func imageTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Does anyone know why my images are not tappable and turning fullscreen?
Make sure both the image view have userInteractionEnabled = YES and
here's an example for what you are trying to do here https://github.com/abhinavsingh77/ImagePreview

About UITapGestrueRecognizer in Swift

I have made a view subclass of UIView.Then, I added UITapGestureRecognizer to it but when I tap on the view, it don't respond to it.
Code:
let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler:")
self.maskView!.addGestureRecognizer(tapGesture)
selector:
func singleTapHandler(recognizer: UITapGestureRecognizer) {
print("xxx")
dismiss()
}
Ensure that you have enabled user interactivity:
self.maskView!.userInteractionEnabled = true
Make sure the maskView on top of the view, and the frame not equals to CGRectZero
Check if you added UIGestureRecognizerDelegate to your view, and use that tapGesture.delegate = self
Your tapGesture should be like let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler"), there is no need of :
Then your function can
func singleTapHandler(){
//action
}
Lastly, make sure you tap on the view you added the tap gesture.

How do I implement a UIGestureRecognizer in my app in Swift?

Lets say I have an image of a tree and when I tap on the screen it changes to an image of a car how would I do this? This is the code I have so far but its not working. (I have three images named "gp1", "gp2", and "gp3" and all of them have different number of taps to change the image.)
I'm in Spritekit and using Swift.
class GameScene: SKScene, SKPhysicsContactDelegate {
//Create Touch
createTouch()
}
override func didMoveToView(view: SKView) {
func createTouch() {
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
gestureRecognizer == UITapGestureRecognizer(target: self, action: "gp2")
gestureRecognizer2 == UITapGestureRecognizer(target: self, action: "gp3")
gestureRecognizer3 == UITapGestureRecognizer(target: self, action: "gp1")
}
}
As Mundi said, for each UIImageView, make sure userInteractionEnabled = true. Otherwise, users won't be able to tap the image. Then, create the gestureRecognizer. Set the action to the method that will handle the gesture. Next, add the gestureRecognizer to the UIImageView. Finally, write the method to handle the gesture.
#IBOutlet var gp1: UIImageView!
#IBOutlet var gp2: UIImageView!
#IBOutlet var gp3: UIImageView!
func createTouch() {
// maker sure userInteractionEnabled is true
gp1.userInteractionEnabled = true;
gp2.userInteractionEnabled = true;
gp3.userInteractionEnabled = true;
// create the gesture recognizers
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer3 = UITapGestureRecognizer(target: self, action: "tapGesture:")
// set the number of taps
gestureRecognizer1.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
// add the gesture recognizer to the UIImageViews
gp1.addGestureRecognizer(gestureRecognizer1)
gp2.addGestureRecognizer(gestureRecognizer2)
gp3.addGestureRecognizer(gestureRecognizer3)
}
Since these are all tap gestures, I've decided to use one method and a switch statement. You could, however, create different methods for each gesture if you wanted to.
func tapGesture(gesture: UITapGestureRecognizer) {
switch gesture.numberOfTapsRequired {
case 1:
gp1.image = UIImage(named: "car")
case 2:
gp2.image = UIImage(named: "car")
case 3:
gp3.image = UIImage(named: "car")
default:
break
}
}
You need to add the recognizer to the view and implement the action handlers gp2 etc.
For UIImageView you should also make sure userInteractionEnabled is set to true.