Add item to UIView without blocking recognisers - swift

How do I add a UIButton, UIImage, UILabel etc. without blocking any function the widget behind is set to call?
For example, I may have a UIButton, and on top of it, a UIImage (which will be smaller than the button, you would still be able to see and tap the button). How do I make it so when the UIImage is tapped the button behind's function is triggered? Note: I don't want to programme the image to have the same function as the button, nor do I want to simply add an image to the button. I want it to be on top.
I'm using Swift 5, Xcode 10.2, and MacBook Pro.
Thanks!

I would have one main UIView which would include your UIButton as well as UIImageView and then I would just add UITapGestureRecognizer to the main view to handle when view gets tapped.

Related

Add selector to button on UIScrollView views added dynamically

I have an UIScrollView with n views added dynamically from metadata stored by archiving. I have a PressGesture to make wobble animation (like iOS deleting apps way) and i want to add a button to every subview on the ScrollView for deleting it from the ScrollView and from files.
My problem is for adding the target to the buttons. When they are pressed, the selector (on the UIViewController parent of the UIScrollView) are not called.
How can I get this done? Any other approach is suggested?
Thanks in advance.
When you animate the view it stops responding to user input. And if your button is located inside animated view, it will definitely not call the selector.
The solution would be to wrap your wobbling view into a transparent superview and then place Delete button inside that superview. If you need your button to be also "wobbling" then you need to put an image that represents button inside your wobbling view. And inside wrapper put a transparent Custom button, that will in fact react on user tap.

uilabel inside a uiview -- adding gesture to the label not owrking

I have a uilabel (and other labels as well) inside a uiview.
I added a gesture that when the label is tapped on I perform some ibaction.
The action is not triggered, however if i place the label outside the uiview (meanning it's not a child of the uiview) than it works fine. I placed it back inside the view than it's stop triggering the action.
any idea why?
something when the label is inside the uiview makes it not react to the gesture and I'm not sure why (despite the fact that both the uiview and the label have 'User interaction enabled).
TIA
Or just not have a UILabel and have only the text of the button be visible, that way you dont have to have a UILabel and a UIButton (from a memory perspective).
I think the problem here is that the UILabel is receiving the touch event instead of the UIView. Try adding the following code (modified to fit your label, of course) to the viewDidLoad method:
myLabel.userInteractionEnabled = FALSE;
This should allow touch events to pass through to the UIView. Why are you doing this? Might it not be easier to work with a UIButton?
apparently there was an issue with the parent control, I redrew the View and it worked fine. Thanks for your comments.

Detecting tap on the UIImageViews added to UIScrollView

I am developing an application in iPhone which has a UIScrollView to which I have added an array of image views (like thumbnails,using loop). The User can scroll either way horizontally and select a particular image. The image on which the user taps has to displayed in a separate UIImageView. I have achieved till the detection of tap on the UIScrollView.But I dunno how to determine which image received the tap so that I can show a preview of that in the UIImageView. How to achieve that ? Any help on this would be appreciated ! Thanks in advance.
When I meet UIScrollView and UIImageView.I always use UIButton instead of UIImageView and use setTag to get which button is pressed.
How do you capture the tap? If it's done by UIGestureRecognizer then you should not forget that a gesturerecognizer can be added only to one object. So you can not catch taps on multiple objects via just one gesturerecognizer. So, in your loop where you create those imageviews also create a UITapGestureRecognizer bound the that imageview.

Is it possible to click and drag the title from one UIButton and drop it into the title of another UIButton?

I have two UIButton's and I want to be able to have the user click one of the UIButton's and drag it over to another UIButton and have the title of the UIButton moved from the first UIButton to the second one. Is that possible? Thanks.
The title is an element of UIButton, so you can't use any predefined methods to do this. You'd have to create custom implementation where you added a draggable UILabel on touch, then set the button's title to match on release - or something like that.
I have created another answer that will at least get you on your way, it implements the press, the drag, and the up events for a button, where you are trying to recognize another button that you have dragged to!!!
Xcode iOS push down button and drag then up on a second button
Look at the answer provided by me!

How do I programmatically make clicking on a UIImageView do something on the iPhone?

I have programmatically added a UIImageView. How do I make it send a message to my controller when the users clicks it, sending itself as the only parameter?
I wouldn't use a UIImageView unless there was some other compelling reason. I'd just use a custom UIButton and use the UIImage as its background.
Create a new class that extends UIImageView and use that.
In your class, override the touch events - touchesBegan, touchesEnded and/or touchesMoved as appropriate
From those methods, call back to a method on the controller.
I'd just put a custom button over the UIImage view, and use normally - buttons with Custom style are invisible and so will not obscure the image. It's also a great way to turn regions of text into clickable areas as well.
Or, you could assign the custom image as a graphic background for the button.
just put a button over the imageview, with the Alpha attribute set to 0.10 or less.
you can capture the button's tap event but almost cannot see it.