Create UIView with custom shape - ios

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.

Related

Multi-colored Shadow Swift

In the iOS music app, I've noticed the shadow underneath album artwork changes based on the color of the artwork. This shadow can also be multi-colored based on how the color at the edge of the artwork changes.
Does anyone know how to recreate this effect programmatically?
Thanks to #Josh Homann for pointing me in the right direction, but the answer was actually much more complex.
The first (bottom) UIView in the hierarchy should have a plain white background and be pinned to the edges of the screen.
The next view should be a UIImageView with an alpha of 0.75.
Then you should add a UIVisualEffectView with the same dimensions as the first view.
Finally, add another UIImageView that is the same size as the first UIImageView.
Your IB hierarchy should look similar to this:
The final result is this:
Its not a shadow its a UIVisualEffectView with a blur. Add a second image view under your image view with a larger size and add a blurring visualEffectView to it.
Sorry to revive an old question, but this library nailed it: ShadowImageView.
It uses CIFilter which gives you more control over the blur effect and better results.

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?

Cut out image of UIView to reveal background tint

I currently have a main UIView that I have subclassed and set a custom background colour from a UIImage.
I also have a .png with a transparent background - lets call it myImage.png.
I want to be able to add the myImage.png to the main view so that I am able to animate the non-transparent portion of my UIImage. I assume that this could be achieved with the use of layers and image masking but I would really appreciate an example to get me started.
EDIT
To clarify, I am interested in knowing how I can use core graphics to cut out the myImage.png and reveal a tint colour. I can then animate this tint colour to give a flashing effect. I am able to animate the image without a problem, so I need a solution to the problem of 'cutting out' the image and revealing a background tint that I my animate in and out.
I am using iOS6
Thanks in advance.
You can play around with CAGradientLayer, CATransformLayer, CAReplicatorLayer.
As I understand your question you have a .png with transparent background and you want to animate the non-transparent part of this .png (myImage.png) you want to add myImage.png to your main view with an animation. Take a look of this apple documentation link of Core Animation
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html
It will help you with what you want

Overlaying view with remove button

I have searched high and low, and maybe because of not defining the problem correctly, I can't seem to figure out how to achieve the following:
I want to add a overlaying transparent view with a remove button. Any thoughts or tutorials I can use?
I have looked at the following but I want to have it fullscreen with a remove button:
How to Implement a Cool Alert/Information Overlay on iPhone?
Just create a new UIView (let's call it OverlayView) in interface builder, with the size of the main view, and above the others, with clearColor background color. Inside that UIView put an UIImageView with the overlay image (it has to be a semi transparent png) and UIButton for closing (on tap remove the OverlayView from superview)

Can I draw the background of a UINavigationBar as the background of my custom view?

I have a custom view which I would like to look like a UINavigationBar. Is there a way for me to draw the background the same way a UINavigationBar would?
I don't want to draw an image or a gradient fill that looks like a UINavigationBar - I want to use the same library code (if it is public) that a UINavigationBar does to draw its background.
The drawRect: method of a UINavigationBar is private - you can't use it to draw the background of your own view.
The easiest way round this would be to take a screen shot, trim the image to nav bar size, add to your project, and then either draw the image in your view's drawRect: method, or add a UIImageView as a subview of your view.
Alternatively, would it be possible to use a UIToolbar instead of your custom view?
You could set your view background image and just use an image that has the navigation bar look already filled in.
As far as tapping into whatever the navBar uses to create itself, you are probably out of luck unless you just build a navBar into your app. Otherwise you would end up having to build a custom view at the top of your app, draw everything in manually. At that point, you are probably better off implementing the navBar.

Resources