Встреча №1. Разработка кастомных контролов для OS X,...
-
Upload
cocoaheads-moscow -
Category
Software
-
view
149 -
download
1
description
Transcript of Встреча №1. Разработка кастомных контролов для OS X,...
![Page 1: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/1.jpg)
Custom UI for OS Xby Dmitry Obukhov
for CocoaHeads Moscow with ♥
![Page 2: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/2.jpg)
• Modify the Look and Feel
• Change and extend behaviour
• Create new controls from scratch
Custom UI
![Page 3: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/3.jpg)
Application Kit...the modern framework for building great apps!
![Page 4: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/4.jpg)
NSViewNSView & Custom Drawing- (void)drawRect:(NSRect)dirtyRect
{
// Your drawing code here
}
NSResponder & Custom Behaviour– (void)mouseDown/Dragged/Up/Moved/Entered/Exited:(NSEvent *)theEvent;
– (void)scrollWheel:(NSEvent *)theEvent;
– (void)keyDown/Up:(NSEvent *)theEvent;
![Page 5: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/5.jpg)
NSControl
NSCell : NSObject<NSCoding, NSCopying, NSUserInterfaceItemIdentification>
The NSCell class provides a mechanism for displaying text or images in an NSView object without the overhead of a full NSView subclass. It’s used heavily by most of the NSControl classes to implement their internal workings.
+ (Class)cellClass;
+ (void)setCellClass:(Class)class;
– (id)cell;
– (void)setCell:(NSCell *)aCell;
NSCell
![Page 6: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/6.jpg)
NSCell
NSButtonCell
NSCell
NSMenuItemCell
NSPopUpButtonCell
NSButton
NSControl
NSPopUpButton
...
NSSegmentedControlCellNSTextFieldCellNSSliderCell
...
![Page 7: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/7.jpg)
NSButtonCell- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { [self drawBezelWithFrame:cellFrame inView:controlView]; [self drawInteriorWithFrame:cellFrame inView:controlView]; }
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { [self drawImage:self.image withFrame:[self imageRectForBounds:cellFrame] inView:controlView]; [self drawTitle:self.attributedStringValue withFrame:[self titleRectForBounds:cellFrame] inView:controlView]; }
![Page 8: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/8.jpg)
- (void)drawBezelWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { // Draw background and border here }
- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView { // Draw image here }
- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView { // Draw title here }
NSButtonCell
![Page 9: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/9.jpg)
- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView { // Draw segment background and border here }
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { // Draw control background and border here }
NSSegmentedCell
![Page 10: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/10.jpg)
Cocoa Drawing
CGNS
![Page 11: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/11.jpg)
Cocoa Drawing
![Page 12: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/12.jpg)
- (void)drawRect:(NSRect)dirtyRect { static NSGradient *glassGradient = nil; if (glassGradient == nil) { glassGradient = [[NSGradient alloc] initWithColorsAndLocations: [NSColor colorWithCalibratedWhite:(253.0 / 255.0) alpha:1.0], 0.0, [NSColor colorWithCalibratedWhite:(242.0 / 255.0) alpha:1.0], 0.45, [NSColor colorWithCalibratedWhite:(230.0 / 255.0) alpha:1.0], 0.45, [NSColor colorWithCalibratedWhite:(230.0 / 255.0) alpha:1.0], 1.0, nil]; } [NSGraphicsContext saveGraphicsState]; [[NSColor windowFrameColor] set]; NSRectFill(self.bounds); NSRect backgroundRect = NSMakeRect(0.0, 0.0, NSWidth(self.bounds), NSHeight(self.bounds) - 1.0); [glassGradient drawInRect:backgroundRect angle:-90.0]; [NSGraphicsContext restoreGraphicsState]; }
Cocoa Drawing
![Page 13: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/13.jpg)
Tips & Tricks
![Page 14: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/14.jpg)
•Use less views
•Draw less - (void)drawRect:(NSRect)dirtyRect;- (BOOL)inLiveResize;- (BOOL)preservesContentDuringLiveResize;
•Be opaque- (BOOL)isOpaque;
•Cache- (void)cacheImageInRect:(NSRect)rect;
Performance
![Page 15: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов](https://reader037.fdocuments.in/reader037/viewer/2022102706/55820dadd8b42aa9498b5508/html5/thumbnails/15.jpg)
•Enabled/Disabled - (BOOL)isEnabled;
•Focus - (BOOL)becomeFirstResponder;
•Active/Inactive - (BOOL)isKeyWindow;
•Someone uses a graphite theme - (NSControlTint)currentControlTint;
- (NSColor *)colorForControlTint:(NSControlTint)tint;
State & Behaviour