Movable UIButtons - ios

Scenario:
There is an UIView as a sub view of a UIScrollView and there is a UIImageView as a sub view of the UIView.
Requirement:
When the user touches on the UIImageView I want to add a UIButton as a sub view at that touch point. Any amount of UIButtons could be added (Within the UIImageView frame). These UIButtons should also be movable inside the UIImageView frame.
What I have done
I have sub classed the UIView and I'm detecting touches using methods touchesBegan, touchesMoved, touchesEnded and adding UIButtons. UIButtons are added with UIGestureRecognizers with a method implemented to the pan gesture.
Problem
When I add more than one button the earlier ones becomes non-movable and doesn't even recognise touch up inside.

Thank you all for the help. I ended up doing this https://github.com/Tulakshana/TTaggableView

You should read about Gesture Recognizers:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
Start with UITapGestureRecognizer for showing UIButtons.

Related

Zooming on content view inside of scrollview doesn't work

I'm creating a scrollview with content view inside of it. inside of the content view there are buttons.
from this point, i already can zoom and pan on content view. but when the touch happen on a button the scrollview doesn't zoom.
I have just had the same problem and solved it with the following steps :
Create a UIView.
Create a UITapGestureRecognizer
Add the UITapGestureRecognizer to the UIView to handle taps.
Create a UIButton.
Disable User Interaction (isUserInteractionEnabled = false) on the button.
Add the UIButton as a SubView of the UIView.
In the UITapGestureRecognizer tapped selector, handle the UIButton touchesUpInside call.
Add the first UIView into the UIScrollView.
Make sure the UIScrollView delay content touches delaysContentTouches = true
The UIScrollView should be able to now seamlessly zoom and you can respond to your buttons.
You can skip the button altogether if you want and just handle the Tap in the Gesture Recognizer. I had to do it the way above because the buttons were a custom sub classed buttons and had a lot of additional custom rendering and functionality included.

Drag Drop UIImageView into subView - Swift 3

I'm newbie to Swift and trying to drag a UIImageView into UiView, I have three UIViews i have to drag the UIImageView into any three UIViews , not anywhere.
I have tried touchesBegan, touchesEnded method, but drag drop is not on inside this any of 3 UIViews.
Please anyone give me skeleton to drop the UIImageView to anyone of three UIViews,
UIImageView and all UIViews probably share the same superView, if you want to put UIImageView inside of UIView you need to move it. I mean you need to remove it from superView and add it to the view beneath it (if they intersect)
Here's what you're gonna do:
When User touches (pans) the UIImageView, move it to superView (If It's already there, it's ok, if It's in one of UIViews, you need to remove it from them, and add it to superView)
When user touch has ended, UIGestureRecognizer will also inform you about it, at that moment check if there's any UIViews beneath your UIImageView or not, if not do nothing else remove it from superView and add the view beneath it.
There's also a WWDC video which covers advanced touch handling techniques.

UISwipeGestureRecognizer over UIButtons

I want swipes to be detected on the lower half of the screen. That part of the screen can have UIButtons as well. I want the swipes to take priority over the buttons (that is, if you swipe over the buttons, the buttons are ignored). But I want the buttons to work if you just tap on them.
I tried placing a transparent view over the buttons with the UISwipeGestureRecognizer attached, but obviously that makes the the buttons not to work as it swallows all touches including taps.
I also tried placing the transparent view behind the buttons, but then the UIButton event handling interferes with the swipe when you swipe over the button.
What's the best way to get this working?
I can think of a solution where the buttons would just be UIImageViews with attached UITapGestureRecognizers so you can then prioritize between the tap recognizers and the swipe recognizers.
But I'd like to keep the buttons as UIButtons to take advantage of the highlighted stuff.
You may try adding the UISwipeGestureRecognizer to the superview of the UIButton.
i resolve same issue.
you just subclass UIButton and override 'touches~' method.
and then connect your customButton to it's parentView via some delegate.

Touches lost on UIScrollview

I am using a UIView subclass which can be resized by dragging on the borders and it is added as subview on a UIScrollView. I am also using a UIGestureRecognizer subclass to move the view on the UIScrollView such that the custom view remains visible and the scrollview autoscrolls.
This setup is working as intended but for one problem. Whenever the custom view is resized, the touches on UIScrollView are lost, i.e the scrollview does not recognise the tap or scroll on it unless the custom view is tapped or dragged once again. I have found that after resizing, neither custom UIView nor custom UIGestureRecognizer's touches began, cancelled or ended are called.
Where might the problem be?

How to get event touch move of UIView when move finger from outside view

I have a UIView custom. In this UIView custom, I have a small UIImageView. When I move finger through it, it will move with my finger. To do that, I use UIPanGestureRecognizer (add this gesture to UIImageView). But when I move finger with begining point not in UIImageView, it will not handle this event.
So, my question is "How to get event touch move of UIView when move finger from outside view ?"
Thanks.
Best approach is create a uiview with necessary size(which is larger than uiimageview). Place the uiimageview inside it. Add gesture recognizer to uiview instead of uiimageview. In the delegate method of geature, move the image view.
There is a "Touch Drag Enter" Event which might be what you are looking for.

Resources