Recognize a touch for a UIView that is only created at the moment that touch occurs - ios

I created a "slide view" (a UIView subclass) which animates on screen by dragging it up. The animation and everything else related to the animation works perfectly fine. This question targets only the very first touch on the screen when the slide view itself will be initialized:
The slide view itself uses the UIPanGestureRecognizerto recognize touches. The thing is, my slide view will be initialized only at the time when the user touches down a UIButton. Parts of the slide view are initially locates on that button, so that when the user touches that button, the touch is also located inside the slide view's frame.
I only want to create the view at the time the touch occurs, because the view is pretty heavy. I don't want to waste resources cause often the button is not even used.
How can I make the slide view recognize that first touch that also initializes (and adds it as a subview to super) the slide view itself?

You can check this out for more details:
Gestures
Well and you can add both gesture pan as well as tap gesture. It will definitely work as tap is not the first action of the pan gesture. So no need to wait for tap gesture to fail.
In short you can add both gestures and handle them simply.

Related

How to make a UIScrollView, some UIButtons and UIPanGestureRecognizer work together?

I got a scroll view with a bunch of buttons, which I want to both touch and drag.
To choose if it's a touch or a drag (not drag of the scroll view, "physically" dragging the button), I check the pan gesture recognizer of the button to see how far away the drag would be, if it's over a threshold I snap the button into drag mode and, removing it from the scroll view and adding it to my view controller's view (at the same location, so looks like it's in the same place).
This all works kinda smooth, except when I want to scroll and the pan fires at the same time. I need a way for the pan gesture recognizer to choose, based on velocity, if the scroll view should scroll or the button should be dragged.
The question: Is there a way for a UIPanGestureRecognizer to tell a UIScrollView to continue scrolling and cancel itself?

Scrollview with button(s) that can appear and disappear on tap

I have created a scrollview, to scroll through an array of images, and I have a button and soon to be 6 more on the screen. Now to make the photo nicer, I would simply like to make the buttons disappear and when tapped on again to appear.
Now I know how to do this with just a regular view with simple isHidden, and how to make it reappear when tapped on, but I am largely confused on how to do it on a scrollview because on a regular view, I just put a button over the whole screen but with the scrollview I can't. Any ideas anyone? Still getting better at this one step at a time, and one question at a time.
Here is a screenshot: http://imgur.com/a/Ll6QO
Use a gesture recognizer on the scrollview.
Add a touch gesture recognizer to toggle the visibility of your buttons. Be sure that the gesture recognizer doesn't cancel touches in view, otherwise you won't be able to scroll.

Gesture Recognizer on sub-view outside view's bounds

I'm not sure if this is possible, but I have a view that is able to be dragged around the screen via pan gestures. Once the view is selected, little grippers appear on the corners of the view that allow the user to resize the view. The problem is, those grippers go outside the bounds of the view (they still show up, because clipSubviews is off), but gesture recognizers on those grippers are not firing when selecting the part of them that is drawn outside of the view. Making the view bigger to actually hold the grippers would break a lot of already created logic that is based on the size of the view, so that is a last resort for me.
Is there any other way to get gesture recognizers to work on views that are drawn outside of their parent view?
You could try overriding hitTest:withEvent: in a UIView subclass, and return the gripper view.

How do I pass delayed scroll gestures from a UIButton inside of a UIScrollView?

I have a UIScrollView that contains several UIButtons. Each button is wired up to take an action when the user inputs a touch up event, so they are able to place their finger on the button and it will not be selected until it is raised. Currently, if I made a swipe gesture to scroll the UIScrollView quickly, then the scroll view moves as expected even if the gesture happens directly over a UIButton. HOWEVER, if I hold my finger down too long on a UIButton (about 1 second), the UIScrollView will no longer recognize the gesture and will not be able to scroll until the finger is lifted up.
I am wondering if their is a way to always have the UIScrollView recognize the scroll gesture? Note that this is not an issue if I touch the UiScrollView in a location without a UIButton - it then scrolls as expected.
It may worth a try to let your UIButton respond to UIControlEventTouchDown (maybe with an empty action). I'm not sure if this will work, but conceptually I think it should let the UIButton capture the touch immediately.
(Also make sure you don't enable delaysContentTouches on your scrollview.)
I found the answer to this here: UIScrollview with UIButtons - how to recreate springboard?
Essentially, I had to extend UIScrollView and override touchesShouldCancelInContentView, having it always return YES.

How do I bringToFront a subview when I touch it?

I have a rotating carousel menu made up of 6 UIViews that were added as subViews to self.view. When you rotate the carousel, some subviews are partially behind the subview closest to the user but the problem is that the subview closest to the user may not have been added after the one behind it so when I touch it, the one behind it gets triggered.
My question is, is there a way to programmatically use bringToFront whenever a subview is touched so that it will not matter whether or not it was added first or last to the view.
When you have 2 views responding to touch events and one is in front of the other, the other one will never receive the touch.
Instead of messing around with the event chain (aka subclassing UIView and overriding hitTest…) I'd suggest you reorder the views while spinning the carousel.
The view which appears to be in front should be the topmost view in the view hierarchy.

Resources