13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 7: User Interfaces: The Application Kit 215Listing 7-7. Drawing Gradients// Create a gradient and use it to fill our boundsNSGradient * gradient = [[NSGradient alloc] initWithStartingColor: [NSColor whiteColor]endingColor: [NSColor lightGrayColor]];if ( self.linear )[gradient drawInRect: [self bounds] angle: 0.0];else[gradient drawInRect: [self bounds] relativeCenterPosition: NSMakePoint(0, 0)];With a little persistence and a few coordinates, it’s possible to create even more interestinggradients. You can supply an array of colors that will be spaced out evenly for you or an array ofcolors with one-dimensional coordinates for each in order to space them out for you. One usefulexample of this is to easily draw a drop shadow below something like a title bar, as shown inListing 7-8; this produces the gradient seen in Figure 7-14.Listing 7-8. A Shadow GradientNSRect r = [self bounds];// fill the background[[NSColor whiteColor] setFill];NSRectFill([self bounds]);// the top 40 pixels will be a bar: no border, just a drop shadow in the 40 pixels below itr.origin.y = NSMaxY(r) - 80.0;r.size.height = 40.0;// the nice shadow gradient—this is grayscale, so use a grayscale color spaceNSArray * colors = @[[NSColor colorWithCalibratedWhite:0.0 alpha:0.0],[NSColor colorWithCalibratedWhite:0.0 alpha:0.1],[NSColor colorWithCalibratedWhite:0.0 alpha:0.3]];const CGFloat locations[3] = {0.0, 0.8, 1.0};gradient = [[NSGradient alloc] initWithColors: colors atLocations: locations colorSpace:[NSColorSpace genericGrayColorSpace]];// now draw it flowing bottom to top (clear to dark)[gradient drawInRect: r angle: 90.0];Figure 7-14. A simple shadow gradientwww.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!