Встреча №1. Разработка кастомных контролов для os x,...

16
Custom UI for OS X by Dmitry Obukhov for CocoaHeads Moscow with

Upload: cocoaheads-moscow

Post on 18-Jun-2015

148 views

Category:

Software


1 download

DESCRIPTION

Доклад Дмитрия Обухова на CocoaHeads Moscow. https://speakerdeck.com/stel/custom-ui-for-os-x

TRANSCRIPT

Page 1: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

Custom UI for OS Xby Dmitry Obukhov

for CocoaHeads Moscow with ♥

Page 2: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

• Modify the Look and Feel

• Change and extend behaviour

• Create new controls from scratch

Custom UI

Page 3: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

Application Kit...the modern framework for building great apps!

Page 4: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

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, Дмитрий Обухов

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, Дмитрий Обухов

NSCell

NSButtonCell

NSCell

NSMenuItemCell

NSPopUpButtonCell

NSButton

NSControl

NSPopUpButton

...

NSSegmentedControlCellNSTextFieldCellNSSliderCell

...

Page 7: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

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, Дмитрий Обухов

- (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, Дмитрий Обухов

- (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, Дмитрий Обухов

Cocoa Drawing

CGNS

Page 11: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

Cocoa Drawing

Page 12: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

- (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, Дмитрий Обухов

Tips & Tricks

Page 14: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

•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, Дмитрий Обухов

•Enabled/Disabled - (BOOL)isEnabled;

•Focus - (BOOL)becomeFirstResponder;

•Active/Inactive - (BOOL)isKeyWindow;

•Someone uses a graphite theme - (NSControlTint)currentControlTint;

- (NSColor *)colorForControlTint:(NSControlTint)tint;

State & Behaviour

Page 16: Встреча №1. Разработка кастомных контролов для OS X, Дмитрий Обухов

Thanks!Dmitry Obukhov

[email protected]