quartz-graphics
Can’t add a corner radius and a shadow
Yes, yes there is… If you want both a corner radius and a drop shadow, you don’t turn on -masksToBounds, but rather set the corner radius and set the bezier path of the shadow with a rounded rect. Keep the radius of the two the same: [layer setShadowOffset:CGSizeMake(0, 3)]; [layer setShadowOpacity:0.4]; [layer setShadowRadius:3.0f]; [layer setShouldRasterize:YES]; … Read more
Applying a Gradient to CAShapeLayer
You could use the path of your shape to create a masking layer and apply that on the gradient layer, like this: UIView *v = [[UIView alloc] initWithFrame:self.window.frame]; CAShapeLayer *gradientMask = [CAShapeLayer layer]; gradientMask.fillColor = [[UIColor clearColor] CGColor]; gradientMask.strokeColor = [[UIColor blackColor] CGColor]; gradientMask.lineWidth = 4; gradientMask.frame = CGRectMake(0, 0, v.bounds.size.width, v.bounds.size.height); CGMutablePathRef t = … Read more
Retrieving a pixel alpha value for a UIImage
If all you want is the alpha value of a single point, all you need is an alpha-only single-point buffer. I believe this should suffice: // assume im is a UIImage, point is the CGPoint to test CGImageRef cgim = im.CGImage; unsigned char pixel[1] = {0}; CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 1, NULL, … Read more
How to convert ASCII character to CGKeyCode?
This is what I ended up using. Much cleaner. #include <CoreFoundation/CoreFoundation.h> #include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */ /* Returns string representation of key, if it is printable. * Ownership follows the Create Rule; that is, it is the caller’s * responsibility to release the returned object. */ CFStringRef createStringForKey(CGKeyCode keyCode) { … Read more
iPhone – Draw transparent rectangle on UIView to reveal view beneath
You have to override the top view’s drawRect method. So, for example, you might create a HoleyView class that derives from UIView (you can do that by adding a new file to your project, selecting Objective-C subclass, and setting “Subclass of” to UIView). In HoleyView, drawRect would look something like this: – (void)drawRect:(CGRect)rect { // … Read more
How to fill a path with gradient in drawRect:?
I would clip to the path you want to fill, and use CGContextDrawLinearGradient. Here is a simple implementation of drawRect: as an example: – (void) drawRect:(CGRect)rect { // Create a gradient from white to red CGFloat colors [] = { 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0 }; CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient … Read more
Can’t compile code when working with CALayer
Make sure you also add the QuartzCore framework to your target. Just importing the header isn’t enough. XCode Screenshot
How do I draw a line on the iPhone?
The first step is to define a subclass of UIView, to create a space to draw in. If you’re starting with a new application, the easiest way will be to start with the “Window-based application” template. Then go New File and create an “Objective-C Class” with “Subclass of” set to “UIView”, and give it a … Read more