testing for taps on graphics drawn in a UIView - ios

I have drawn a non-rectangular shape into a UIView subclass using core graphics. Now I want to make this shape "tappable", but not the areas of the UIView that is transparent. IE the UIView subclass background color is alpha 0. When the user taps off of the graphic itself, but still on the view, how do I test this so I can do an appropriate action, or not?

Related

How to get non-rectangular shaped button?

How can I get a non-rectangular shaped button?
I have a UIImage with a mask. How can I set the shape of a button to this image without the transparent color.
I have a UIImage with a mask. How can I set the shape of a button to this image without the transparent color.
Buttons are controls, and controls are views, and views are inherently rectangular, so a button will always occupy a rectangular space in the view hierarchy. However, the visible part of a view (and therefore a button) can be whatever you want it to be... a view can have a transparent background, and can draw itself however it likes. A view can also choose to pretend that a touch event doesn't hit it, potentially making the view seem to have a non-rectangular shape for the purpose of delivering touches. You can do that by overriding hitTest(_:with:).
Also, realize that you don't always need to use buttons in order to interact with objects on the screen. If you have an image of a house, for example, and you want the user to be able to tap different parts of the house to change its color or texture, you could display the house in a view that knows where different parts of the house are in the image. You could use gesture recognizers or the normal touch handling mechanism to let the user interact with the different regions, and those regions can be any shape you like.

Can we hide a UIView partially in iOS

I have to partially hide a UIView when user scrolls over it. There is a UIScrollView above UIView.
For example in the given image below i want to hide the area covered under the scrollable area which is in blue color. All my views background colors are clear color.
I want to hide the part as given in the below image, the marked rectangle which is in red color. So that part of text (One, Two, Three) is only visible.
Each UIView, including UIScrollView, has a Core Animation Layer (a CALayer).
You access the CALayer with
view.layer
In turn, a CALayer has a mask, which you access with
layer.mask
Using the mask is the most comprehensive method of controlling visibility and opacity at runtime.

Creating UIView with BG Image and then drawing on top

what I am trying to do is to have an image on which I then want to draw something by overriding the drawRect function. Essentially I am trying to create dice, where I use a background and then have the relevant dots drawn in depending on the number selected.
I created the dice dots in PaintCode paint-code and then exported that to use in Xcode (Swift).
I am creating a UIView on Storyboard that I then want to add a background image to and then draw the dots based on the number I pass it.
On it's own the drawing of the dots are working fine, but when I try and add an Image using a subview with UIImageView the image doesn't show up, the background of the UIView is white and even setting it to clearColor() doesn't work.
Any suggestions?

gradient background iOS

This rectangular region has a gradient background which is also transparent. Texts can be written on the rectangle. The colour of the rectangle changes when the user touches it. When the user releases it, it takes you to a new screen. How should something like this be implemented in iOS?
Use
UIImageView with gradient backgroundImage
set its alpha to 0.6 -.75 so that background view is visible
Use Tap gesture recognizer on ImageView to change background Image when user tap and navigate to next view

Create UIView with custom shape

So I was wondering if there's a way to make a UIView with a custom shape.
I'm trying to make a piano keyboard so when the user touches the view a delegate method responds by playing a noise. The picture is a .png with the picture of the key and a transparent background.
Thanks in advance.
You can't make a UIView in the shape of, say, a triangle, but what you can do is make the view transparent, then add your non-transparent content to it.
For example, to make a view with a background of a png (with, I assume, partially transparent areas) you could make a transparent UIView and then add an image view to it. better yet, just use a UIImageView in the place.

Resources