UIPanGestureRecognizer is moving subviews while dragging - ios

So I want to add a PanGestureRecognizer to a UIView class that I have made. Everything is working well, however: when I drag the view, it's subviews are also moving/updating etc.
-I am using SnapKit for my view's constraints
-I tried setting translatesAutoresizingMask to false, no result
-I tried to set constant constraints on the subviews,not result
Here is a gif of what it looks like:
https://media.giphy.com/media/oI0Wf5T6B0jIY/giphy.gif
Thanks for the help everyone.

This is expected and is how UIKit (and essentially all 2D/3D scene graphs) are designed to work. The geometry of a view is relative to its parent view. If the parent view moves, so do its decedents.
If you do not want a view to be dependent on the position of the view being moved, it probably should not be a subview of that view in the first place. It's possible to apply the inverse movement to the subviews to make it appear that they're not moving, but that is most likely a lot more work than just not making them subviews in the first place.

Related

iOS - touches not recognized after constraint added

In my iOS app, my parent view has a child view which, in turn, has a number of UIButtons as children. Without constraints, touches on the buttons are recognized by the topmost view controller. However, when I add a constraint to center the child view (the one containing the UIButtons) horizontally within the parent, the parent stops recognizing the touches.
First of all, is this the correct/preferred way to do this (centering a group of views - e.g. buttons - by putting them in a View)? Should I be using a Container View, or stacked Stack Views, instead? (I tried a Container View, but (a) it generated its own controller, and (b) for some reason, I couldn't move the buttons into the container.)
Second, how do I get the view to recognize the touches while maintaining the constraint?
Apparently, the problem was, I needed to set a fixed width and height as well in order to get the layout set up correctly. Once I did that, it worked correctly.

how does one bring superview to front?

Normally developers try to bring subview to front. On contrary, how does one bring superview to front?
I'm looking for a reverse of bringSubviewToFront(_:). It should look like bringSuperviewToFront(_:)
I'm looking for a reverse of bringSubviewToFront(:). It should look like bringSuperviewToFront(:)
That doesn't exist. Instead of giving your button a subview, make both the subview and the button subviews of some container. Then you can adjust their relative z positions to suit your purpose.
I do not think you can do that. Views are laid down in layers with each sub view layer being part of super view layer. The effect you want to materialise can be achieved by hiding/removing all the sub views of the view you want to get Bring To Front effect on.
This works well with sub views because normally you would want to show/hide subviews inside same parent view based on their layer position!
In the spirit of being less rude.
func bringSubviewToFront(view: UIView) {
let superview = view.superview
superview?.addSubview(view)
}
That will bring it to the front. As in:
bringSubviewToFront(mySubview)
What the above code does is detach the view from wherever it is in its superview's hierarchy and reattach it to the top of all the subviews. This has the effect of bringing it to the front like you want. If the view isn't part of a hierarchy, then the function does nothing.

How to zoom on UIView

In my iOS app I want my users to be able to zoom in on the screen. My main uiview contains several subviews which contain images. I want my uipinchgesturerecognizer to either change the scale, or ideally use some "zoom" rather than scaling each subview.
Please and thank you.
This can be accomplished with UIScrollView. First create a scroll view as the base of your view hierarchy, putting your previous container view as a subview of the scroll view. Set the delegate of the scroll view to self and implement the delegate method viewForZoomingInScrollView, in which you should return the view that will be zoomed in (your original container view). This will allow the user to pinch and zoom your original UIView.
It's hard to provide advice on this without having a clearer view of what exactly you want to achieve.
Can you include a link to a sketch? For example, do you want the individual subviews to remain the same size but the layout to change ? Do you want the individual subviews to resize but their contents to be upscaled?
If you simple want to treat the subview as (basically) a single image which just happens to have other images in it, then maybe it would be better to render it as one and then scale that?

Multiple UIView transformation animations, one repeats, the other does not

I have a UIImageView whose instance has a repeating pulse-like animation.
I want this UIImageView to move with the force of the accelerometer. To do this I have added another animation, this time a translation.
However, once this translation happens, it stops the pulse, even when its not animating.
As these two animations are separate, I cannot use the CGAffineTransformConcat method to combine both animations.
I have tried adding one to the UIView and one to the layer property of the same UIView but it does not work.
I have also tried adding the movement directly to the frame of the view, however, this just causes my view to disobey the constraint constraining it to the center x of the superview.
Does anyone use/know of a work around to this?
Thanks as always!

Propagate dragging touch to UIScrollView superview

I have been looking to all the other similar topics here, using UIGestureRecognizers, using hitTest:withEvent, pointInside:withEvent: etc. but nothing seems to be ok for what I need to achieve.
Basically I have a main view (self.view of a common UIViewController) and a small rectangular UIScrollView attached onto it at the bottom: the scrollView is filled with some UIImageViews and the user can scroll it as usual.
But the user should also be able to drag one UIImageView (or a copy of it) from the UIScrollView to the main view, and, this is what I am finding really difficult, with the SAME dragging gesture, hence I need a way to:
1) Distinguish between normal horizontal scrolling gesture, which should be handled by the UIScrollView the usual way and a dragging gesture over the image view.
2) Once identified a dragging gesture, should propagate the touch to the superview, which will host a copy of the UIImageView and WITH the SAME dragging gesture continue the dragging over the main view even out of the bounds of the UIScrollView.
Please note that I know that if the UIScrollView has userInteractionEnabled = NO the touch is propagated to the subviews, but 1) I want to propagate it to the superview not the subviews, 2) the userInteractionEnabled property apparently becomes active only once the initial gesture is terminated, while I need to use a single dragging gesture.
Thank you very much for any help.
So, so far I have ended up implementing the touchesShouldBegin:withEvent:inContentView: method of my UIScrollView subclass but with delayContentTouches set to YES (default) instead of NO as #nhahtdh was suggesting.
Strangely enough even only implementing the method was sufficient for my subviews to intercept the dragging, and still my scrollview is scrolling properly, while with delayContentTouches set to NO I was not able to scroll it as all the subviews were starting to move around.
Really the credit for this is #nhahtdh, so man, if you post an answer I will accept it, thank you very much for your help.

Resources