How to have single tap gesture to multiple Views using storyboard.
I drag 3 views and one tap gesture into UIView class.
Contacted three view to tap gesture and added action class handleGesture Method on tap on any one the tree view it should trigger the method action.
using story board.
But i want to do it with single tap gesture is it possible or not.
try with this, define a variable UITapGestureRecognizer with your method, after that, in a method or wherever you want, you can add this gesture to your multiple views
var recognizerMovements: UITapGestureRecognizer {
get { UITapGestureRecognizer(target: self, action: #selector(self.myActionMethod)) }
}
self.myFirstView.addGestureRecognizer(myActionMethod)
self.mySecondView.addGestureRecognizer(myActionMethod)
Related
I have custom UIView that is being displayed in controller's view. I need to display a view controller modally when something in that custom view is tapped. What is the most efficient way to notify the view controller (if it exists) about the touch from the custom view's touchesBegan()?
You can use UITapGestureRecognizer. Declare UITapGestureRecognizer like this and add gesture recognizer to your custom view
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapOnCustomView(_:)))
customView.addGestureRecognizer(tapGesture)
Now receive the tap
func tapOnCustomView(tap:UITapGestureRecognizer){
// present your targeted view controller modally
}
NB: UITapGestureRecognizer allocation syntax vary with Swift version. This one is for Swift 2.2
I'm adding several views in the code below:
for var i=0;i<sets.count;i++ {
setView=UIView(frame: CGRectMake(0,y,400,65))
x=20
for var c=0;c<sets[i].count;c++ {
imageView=UIImageView(frame: CGRectMake(x,0,60,60))
dieFaces=types[sets[i][c]] as! NSArray
file="\(dieFaces![0]).png"
print(file)
imageView!.image=UIImage(named: file)
setView!.addSubview(imageView!)
x+=60
}
setView!.tag=i
setView!.addGestureRecognizer(tap)
scrollView.addSubview(setView!)
y+=66
}
Only the last view added is responding to the tap. What am I doing wrong?
A tap UITapGestureRecognizer can only be attached to a single view, so only the last view is responding.
You'll need to create a new gesture recognizer for each setView you're attaching it to.
UIGestureRecognizer can be added to only one view. So when you add it to another one it just removes itself from the previous view.
I can suggest two options:
Add recognizer to a superview. In this case it's UIScrollView
Or create more recognizers(one per view) and use the same target and action.
I have an image and in the storyboard I've dragged a Tap Gesture Recognizer on top of this image and changed the settings:
For my TGR:
For my image:
How do I hook this up to my controller now? I want a method to fire off when the double tap happens. Is there some sort of protocol I have to conform to? Do people normally do this from the storyboard or programmatically in viewDidLoad? I don't mind doing it another way if that's the general trend of things.
connect the gesture recognizer as a outlet to your ViewController and in your viewDidLoad :
[self.yourGesture addTarget:self action:#selector(didTapOnImage:)] ;
and then declare your method didTapOnImage method :
-(void)didTapOnImage:(UIGestureRecognizer*) recognizer
{
//do your work here
}
I have added a subview to a View Controller's view. This subview is the view of QLPreviewController.
What I am trying to achieve is to recognize swipe gestures on the subview in the parent view, i.e. the View Controller's view. In the end, I want to be able to swipe left /right on the view to load the next document for preview.
I'm aware of hit testing and understand that by just attaching a gesture recognizer to the parent view, those will not be recognized, since the subview will be the "hit-test" view.
Now what is the best (or easiest) way to recognize those gestures?
Note: I didn't manage to attach the gesture recognizers to the subview, this doesn't seem to work.
* UPDATE *
To make this more clear - this is the code from my ViewController. vContent is just a view in my ViewController, where I add the view of the QLPreviewController:
let pvVc = QLPreviewController()
pvVc.dataSource = self
vContent.addSubview(pvVc.view)
I tried adding the swipe recognizers both to the vContent and the pvVc.view. In both cases no event was fired.
let sgrLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
sgrLeft.direction = UISwipeGestureRecognizerDirection.Left
sgrLeft.delegate = self
On some other view the code works fine.
Any hint is appreciated!
Thx
Eau
Well, the responder chain, the unknown animal … ;-)
You can subclass the superview and override -hitTest:forEvent:.
You rarely need to call this method yourself, but you might override it to hide touch events from subviews.
Gesture Recognizers Get the First Opportunity to Recognize a Touch, so even the subview is hitTest view. the gestureRecognizer attached on superView can recognizer touch event.
I've a view controller with UISegmentedControl and UITableView as its children. In -viewDidLoad I create a UITapGestureRecognizer and add it to the controller's main view.
When I tap on, for instance, a cell of the table view, the tap gesture recognizer's action is being called and it prevents receiving touches to the table view (that's ok as tapRecognizer.cancelsTouchesInView == YES).
However, the issue is that when I tap on the segmented control, the tap gesture recognizer's action is not being called. Why is it so? How to make the tap gesture recognizer the "top" object which handles touches?
Thanks,
Adam