Detecting tap anywhere on widget? - ios

I am trying to detect when a user taps anywhere within my Today Extension widget.
Currently I have a tap gesture recognizer on the primary view which contains all the labels displayed within the widget. However, with this configuration, only taps on the individual labels contained within the view are detected. If a tap occurs within the view, but not on a visible label, the tap does not appear to be detected.
I know this has to do with tap detection not occurring on any visual object that has less than 100% opacity. Unfortunately I cannot figure out how to detect taps on anything that isn't an opaque visual object.
Is there any way to simply detect a tap anywhere within the widgets bounds, including negative space?
Thanks!

Ended up just putting a view that covered the entire background of the widget's container.
Setting the view's fill opacity and the view's opacity itself both to .1% made it effectively invisible while still capturing taps.
:P

Related

How to make different regions of image into buttons (swift/xcode)?

I have a map and I want to turn different regions of it into clickable elements. I know I could just splice up the map using photoshop and turn each region I want into a button individually, but that feels a bit hacky to me and I don't know if the aspect ratio of everything would stay the same from device to device when I piece the puzzle together. What is the best way to take a single image and divide it up into several complexly shaped clickable areas?
The most general-purpose solution is probably to make the entire view (image view) clickable by attaching a tap gesture recognizer to it and then interpreting the tap gesture.
I'd suggest creating a custom subclass of UIView that has an image view inside it, attaches a tap gesture recognizer, and responds to the messages from the tap gesture recognizer to figure out which region was tapped.

Animated UIWebView does not respond to touches during animation

At the moment, I'm animating a UIWebView from the bottom of the screen to the top. As the UIWebView animates upwards it becomes untouchable for 50-80% of the duration time.
If I tap the UIWebviews ending destination, or model layer, the taps are registered and the Webview responds appropriately.
Why is this? And what solutions are there for tapping moving/animating UIWebViews?
To further exemplify this, I have created a side project that shows a UIWebView animating upwards. The purple squares represent touch events on the presentation layer, while the blue squares represent touch events outside of the presentation layer.
https://github.com/AdamBCo/UIWebView-Animation-Issues
As seen in the example, UIViewAnimationOptionAllowUserInteraction is set in the UViewAnimation block.
A UIWebView is complicated layout of web elements. To redraw that during animation there is simply not enough time available to entirely redraw the UIWebView, keep the controls in it available for interaction and do the animation.
However there are some settings for this inside a CALayer on what to do with its contents when animating it. I would start looking into that.

Hidden MKMapView is somehow receiving touches when scrolling UITableview that is on top

I have a UIViewController that uses a segmentedControl to switch between a MKMapView and a UITableView controller. The map shows some locations, then switching to list will show those in list form.
When toggling the segmentedControl it will show the corresponding view and hide the other.
The extremely strange issue I am having is, when showing the tableView and scrolling down, it sometimes stops scrolling and I can just swipe my finger up and down and the tableView does not move. However, I then switch to the map, and it has moved to another location. So I am realizing that somehow when scrolling my hidden mapView is receiving those touches.
How is this possible? And is there a way to prevent this?
You're hiding the view by setting the hidden property to 1, right? iOS treats views with an alpha of below 0.02 as hidden, too. If you're correctly setting it as hidden, the view is not drawn and should not be receiving any touch events - are you sure something else isn't coming into play here?
Have you overridden the touchesBegan method of your MKMapView to see if the map view is definitely receiving touches when it is hidden?
Perhaps try setting hidden to 1 and setting the alpha property of the view you want hidden to 0. If the problem persists, there's most definitely something else going on, as a hidden view is not drawn to the screen at all and thus is not forwarded any touch events by the system.
I figured it out. The issue was a custom callout. When that was open on the map, and I toggled the map hidden, and showed the tableView, somehow that callout was still receiving touches, which would then scroll the map with it.
Hiding the callout at the same time as the map solved the issue.

iOS touch event triggering by movement of other view

Here's the scenario I am trying to implement:
I already have a view that can let user draw doodles by touching inside the view directly (like a doodle canvas). This view implements touchesBegan, touchMoved, and touchEnded handlers to draw lines from touch event parameters.
Now instead of that, I want user to be able to drag and move another UIView on this canvas view and can still draw lines just like they touch directly. For example, user can drag a pen image view on the canvas view to draw pen-style lines.
In this case, how can I transfer the movement of this pen image view to the canvas so it recognize it? One more question: if I want this canvas view to only recognize movements of drag other views rather than touching directly, what I should do?
(Sorry that this question is little too general, just want to get some pointer of it)... Thanks!
A better way to look at the problem is
How can I transfer movement on the canvas into the location of a pen
image view?
That's easy. You already have all the code that keeps track of movement in the canvas (touchesBegan, touchesMoved, touchesEnded), so all you need to do is change to center property of the pen view to track the movement in the canvas. (Obviously, you'll need to apply small X and Y offsets to put the center of the pen view at the correct location).
The only non-obvious detail that you need to be aware of is that the pen view must have userInteractionEnabled set to NO. That way, the pen view won't interfere with touches reaching the canvas view.
Note that UIImageView has user interaction disabled by default, so you don't need to do anything if the pen view is a UIImageView. However, if you're using a generic UIView to display the pen, then you need to disable user interaction in storyboard under the Attributes inspector or disable it in code, e.g. in viewDidLoad.

iCarousel – Make all items selectable, not just centered one

I'm using the great iOS control iCarousel's Wheel type and I want all of the buttons it contains to be usable / selectable at any time (i.e. I can tap any of them, no matter which one is currently at the top of the wheel). The user can still scroll the wheel as normal, but no matter which button they tap, it should register.
The current behavior seems inconsistent: if I tap one of the buttons directly next to the center one (to its left or right), that button moves into the center slot. Clicking one of the buttons two positions away 'sometimes' causes it to scroll to that letter, sometimes it's ignored. Any other buttons are always ignored.
Is this possible to set up, preferably without hugely modifying the class? I'm by no means an expert, but am learning every day :)
Thanks in advance.
Set carousel.centerItemWhenSelected = NO;
That will disable the behaviour where buttons other than the centre one are scrolled to the centre when you tap them.
As for the reason why some are not responding to taps at all, it is most likely because the frame of your carousel view is too small and the tap events are outside of the frame.
If you set carousel.clipsToBounds = YES; it will crop the carousel views to the frame as well so you'll be able to see exactly what size your carousel actually is.

Resources