Is it possible to set the fill color of a CALayer that has already been drawn on screen?
I am using SVGKit to load a SVG image into a UIView and everything is working very well but I need to change the fill color. I have read up on CAShapeLayer.fillColor and think that it can be done easily but I don't seem to see an easy way to do it with a CALayer? I really do hope it can be done.
May be someone might even be able to suggest if a CALayer can be converted to a CAShapeLayer?
Look at the docs for CALayer and you'll see
#property CGColorRef backgroundColor
Related
I have a UIView which draws itself via drawRect. I use core graphics and draw a bezier curve. I would like to have an animation which changes the color of the bezier curve drawn on some occasions. However just having the color as a property and changing it in an animation block doesn't work. I also need to ensure that it is redrawn correctly. What is the way to do this? I'm quite new to IOS
I don't think you can do what you are trying to do. The system has special code that knows how to animate changes in it's animatable properties, but it does not support animating changes to any other properties.
To animate a color change, you're going to need to use a CAShapeLayer to do your drawing. the strokeColor and fillColor properties of shape layers are animatable.
I have a UIImageView with userInteractionEnabled set to YES, and a few gesture recognizers on it which let the user interact with it. The view's image is of a shape, with the rest transparent, and I only want the opaque parts of the image to accept touches (the superview is also interactive).
I'm guessing I need to override a hit testing method, but which one and how?
You can obtain the the pixel color information on the touched point ( How to get the RGB values for a pixel on an image on the iphone ). May be you can test for its alpha value to determine whether it is opaque.
Although this isn't the exact answer you're after, this should give you an idea of one way of achieving this. This blog entry is using the Cocoas2D framework.
http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/
I have a button and its type is UIButtonTypeRoundedRect, but its border does not go along the rounded corner. How can I curve the border.
Check out this blog post I recently wrote: UI Polish in An Instant
You're on the right track accessing the button's layer property, but there is more you need to do (for example, to get rounded corners, add deleteButton1.layer.cornerRadius = 10), and more you can do, all without extra images.
Images are the recommended method for creating custom buttons. Apple's built-in buttons are basically only there for prototyping.
You could also create a subclass of UIButton and then override the methods for drawInRect and provide custom drawing code, probably using CoreGraphics. However, it is still much cleaner in code and more efficient at runtime to just use images.
I have an UIView here that I'd like to partially fade the bottom of. Basically I want the alpha value to decrease the further down on the UIView we go. So the top of the UIView is completely visible, while the bottom part has an alpha value of 0.
Anybody got any suggestion as to how to accomplish this?
CALayer has a property called mask, which is designed for doing precisely this sort of effect. If you assign another CALayer to the mask (one that has no superlayer), the alpha channel of that second CALayer is used when compositing the first CALayer.
Be careful, though, as using the mask property has a significant performance hit.
I want to set the background of an NSBox to be a gradient. In Interface Builder it is possible to set the background color of an NSBox to selectedMenuColor which is a gradient.
NSBox only has a setFillColor method so how is Interface Builder filling it with a gradient?
How do I programmatically fill an NSBox without subclassing it? It would be trivial to subclass NSBox but the workings of Interface Builder suggest there may be better solution.
selectedMenuColor is a "magic" color that is not displayed as a solid color. Many of these "magic" colors exist in the system.
I have used colorWithPatternImage: for this before. But note that the image you use as the pattern will get tiled, so you will probably have to resize the image to the size of the box.
Probably the closest you could come would be to use an NSColor created with colorWithPatternImage:, then create the gradient you want as an image and load that in. Ugly, but should work. I think subclassing is your best bet.
The selectedMenuColor color is actually a pre-rendered image of a gradient, and not a gradient drawn on the fly, so there is not any way to specify an arbitrary gradient as a background color. Like Ben said, subclassing is probably the way to go.
In xib, select NSBox, then goto effect inspector, check NSBox for Core Animation Layer.
Now
IBOutlet NSBox *box;
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor whiteColor] CGColor]];
or
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor colorWithPatternImage:[NSImage imageNamed:#"white.gif"]] CGColor]];