Sending touch events from view to controller - ios

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

Related

Swipe left view controller to dynamic view

I'm currently building an app that needs to be able to swipe left to the next view controller and then show random quotes dynamically from a dictionary.
I've added the current gesture recognizer from the initial view controller however I don't want the standard push segue functionality.
How it should function:
From swiping left you are swiped to the next view controller. From this view controller I could use a scroll view. Could I generate dynamic sub views based on the count of the dictionary or is there something else.
Current gesture recognizer:
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "goSwipe:")
swipeLeft.direction = .Left
self.view.addGestureRecognizer(swipeLeft)
I would like to be able to swipe left on the inital view controller to the next (there's only 2 VC's in the app).
Thanks
you can use horizontal scrollview by setting down its contentSize method x position then generate dynamic sub views based on the count of the dictionary.
Check this out here.

Single tap gesture to multiple View using storyboard

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)

Recognize swipe gesture in view not in subview

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.

how to get touches of uiimage view in scrollview and perform some action

I have a scrollview with uiimageview1 added on it. On click on that uiimageview1 I want to present a viewcontroller to make some drawing. After drawing, I want to take picture of that drawing and place it on uiimageview1.
i don't know how to and where do i present that viewcontroller for drawing.
please help.
So, first you need to detect touches on your imageView. To do that you need to use UIGestureRecognizer or UIButton.
Then, you need to present a new view controller using the presentViewController:animated:completion: method of your current UIViewController (passing it the 'drawing' view controller).
After the drawing is performed, you can use iOS delegation pattern to notify the original view controller that you just drew something.
The original view controller handles that 'delegated event' and updates the image view accordingly.
UIViewController and UIButton documentations are here for you.
Create a UIButton with uiimageview.layer.bounds and add it as subview to uiimageview
Make the button background color [UIColor clearColor]
Add a #selector to button and push newViewController with segue or add it programaticaly from UINavigation Controller stack.
Otherwise you can make a pan gesture recognizer to UIImageView and listen for user interaction TAP on screen for pushing the new view controller
You can Simply do this by adding UITapGestureRecognizer properties to your image view.
Like:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(presentViewController)];
[self.yourImageView addGestureRecognizer:tapGesture];
-(void)presentViewController{
// do remain stuff
}
Happy Codding :)

UISplitViewController swipe gesture interferes with other swipe gesture

I'm using a UISplitViewController and one of the detail view controllers contains a view which has a UIPanGestureRecognizer added to it. When I swipe this view in the detail view controller, the gesture is recognized, but the swipe gesture recognizer of the split view controller interferes with it; the master view controller is displayed and the gesture recognizer from the detail controller is ignored.
Implementing and debugging the shouldRecognizeSimultaneouslyWithGestureRecognizer method from the UIGestureRecognizerDelegate shows two UIPanGestureRecognizer objects: one from the detail view controller and the one from the split view controller, so I'm certain they are interfering with each other.
When I set presentsWithGesture = NO on the split view controller, the gesture recognizer inside the detail view controller does work. But that disables the gesture recognizer on the split view controller, so it's not really a solution to the problem.
I've also tried disabling the gesture recognizer on the split view controller, only when I need the other gesture recognizer to work, but it seems that it is not possible to set presentsWithGesture once the split view controller has become visible.
I've also tried to disable the default gesture on the split view controller and adding a custom gesture which I can control, but it didn't work. I tried using the target and action from the split view controller barbutton on the gesture, but it doesn't work. Calling toggleMasterVisible: on the split view controller isn't an option either because it's part of the private api.
Does anyone have any suggestions on how to handle this?
I would suggest that you disable the UISplitViewController pan gesture when you need the other one to work. This should do it:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[recognizer setEnabled:NO];
}
}
You probably don't want to search for it each time, so I would store a reference to that gesture recognizer when the view loads, then just disable and enable as appropriate:
on viewDidLoad:
for (UIGestureRecognizer* recognizer in [splitViewController gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[self setSplitViewPanGesture:recognizer];
}
}
later:
[self.splitViewPanGesture setEnabled:NO];
and later:
[self.splitViewPanGesture setEnabled:YES];

Resources