Send user action to parent uiview in iOS - ios

I am working on user interaction in different uiviews.
I know how to send user interaction on parent view or on it's specific UI components.
In my example, I am sending event to UIButton that is working properly even I tap on outside of UIButton bounds (Please take a look on attached code for more inside).
But I don't know that when I tapping on top view, UIButton TouchDown selector called but TouchUpInside not calling. Why is it happening?
Any suggestions? Any explanation is greatly appreciated!
Github code link: https://github.com/jackMac1811/iOSUIInteractionTest

All uibutton events carrying their own behavior which enables to execute its bunch of code according to it.
if you want to invoke both methods tapping on top view you should have to use touch up outside instead of touch up inside event
Here's a very useful link https://stackoverflow.com/a/11390048/4003548
Hope this helps ..

Related

UIScrollView pass event to child chain on WillEndDragging

Edit: I am editing my initial question (see below for history) as I am getting new information.
I figured out that when the swipe motion starts from inside the button bounds, we never receive TouchesEnded or TouchesCancelled, only TouchesMoved. However, if I can react on WillEnddragging, it would be great. Is it possible to cancel a gesture on WillEndDragging and also pass this cancel down the children chain?
History:
I am using Xamarin Forms and I have the following issue
I have custom controls part of native scrolling views, like ScrollView or CollectionView, that remain in "clicked" state after the finger enters them but then initiates a scroll gesture.
I had a similar issue on UWP in the past and managed to solve it with the UIElement.PointerCaptureLost event.
Sorry if I am wasting your time on trivial stuff, but I am really stuck and I greatly appreciate your help.
I have tried different approaches suggested, including setting DelaysContentTouches to NO, and playing around with CanCancelContentTouches and overriding TouchesShouldCancelInContentView to always return NO, in a ScrollView custom renderer.
I have had a read of
Allow UIScrollView and its subviews to both respond to a touch
and
UIScrollView sending touches to subviews
Maybe the accepted answer here helps, but I am not sure how to get the tag of my custom view.
What I am expecting is my custom controls to receive the cancelled touch event (or something similar) as happens in both Android and Windows
This was easier than it looked. Solved by adding a UIGestureRecognizerDelegate to my UIGestureRecognizer class and in the delegate I overwrote ShouldRecognizeSimultaneously to return true.

iOS - change animation when user taps screen

Lets say that i have an animation - an image is going from left side of the screen to the right. I would like to make it a little bit interactive - when user taps on a screen i want to change direction of image movement. Whats the best approach to implement it?
What I do in some cases is take the main view of the View Controller, in Storyboard, and change the class type of that UIView to UIControl.
In the code that is accessed as MyViewController.view, which you can write:
var viewAsControl = myViewController.view as UIControl
In Swift or some equivalent of that.
The UIControl subclass of UIView is the hierarchical layer (class) that adds the action/target facilities to a view. For example, UIButton is a UIControl, because it generates events (actions), and it is also a UIView so it can be added as a subview.
Then from the Connections Inspector, accessed via the far right Icon of the far right panel (that is, the panel to the right of the storyboard editor window), I'd select the Touch Up Inside event type or some other event and drag it to an #IBAction tagged function I'd add to the View Controller's source code, to receive the tap event. From that tap notification, you can cancel the current animation and add a new one, etc...
Alternatively, you can create an IBOutlet for the view if you've turned it into a UIControl in IB, and use the addTarget() method to assign an action handler for a specific event, e.g. to make it call a function in your code.
Either way the effect will be that any time the view is tapped, it will generate the event for you to respond to

UIButton does not detect Touch Down right away

I created UIButton through interface builder. That button has Touch Down event and Touch Up Inside event on it which triggers necessary code to be executed. Somehow the Touch Down IBAction linked to that button is not getting called right away I touch the button. I have to move my finger little bit before that action gets called. Did anyone face same kind of issue ?
Is it because I have two IBActions assigned to the same button ?
Thanks in advance.
Sounds like you linked your action to the wrong control event, specifically, it sounds like you linked the action to one of the Touch Drag... events.
Make sure that you hook your action to the button's Touch Down control event.

weird ios touch event passing with UIGestureRecognizer

hi i am observing something weird in iOS custom views, where i have a button in side a custom view, i am looking for documentation or your answer to explain why i have this observation
i have a custom view (subclass of UIView), it contains a simple button, i bind the button touch event via addTarget:Action with UITouchUpInside, i just bind it selector to a simple method that NSLog a message
if you display the custom view and hits the button, you can see the message in the console, everything works as expected.
However, if you add a UIGestureReognizer to the custom view, and run it, when you click the button, it no longer prints the message, WHY???
i did more in depth investigation, and by looking at the hittest method IN THE CUSTOM VIEW (the view contains the button) (i override it, but i didn't do anything, i just call super again), the hittest is returning the correct view, i.e. when i click on the button, hittest method of the custom view is returning UIButton
i do not understand the event chain here, can someone pointing the documentation that explains this?
it seems to me when you add the regonizer to the container view of the button, for some reason, the button DOES NOT KNOW how to handle the event, so it asks its superview (custom view in thsi case) to handle it, but why?? i already bind the event handeler to the button via UITouchUPInside
please please help i want to understand this
Set your recognizer's cancelsTouchesInView property to NO.

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources