Upload
geekslab
View
96
Download
4
Embed Size (px)
DESCRIPTION
Popups, alerts and windows Константин Чернухо Яндекс Беларусь Как красиво и безболезненно создать кастомный попап на iOS на основе UIWindow, и при этом не иметь проблем с вращением девайса. Особенности поведения UIAlertView. Как сделать поведение кастомного попапа идентичным системному. Все это рассматривается в контексте существующих готовых сторонних решений, их плюсов и минусов.
Citation preview
1
Kanstantsin Charnukha iOS developer
Popups, alerts and windows
2
Goal
• Fullscreen popup • Overlaps status
bar • Customizable
button color • Customizable
button title color
3
Customizable transition animations
4
Custom popup VS UIAlertView
5
Customization items
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
6
UIAlertView сustomization by using its properties
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
7
UIAlertView сustomization by using UIAppearance
UI_APPEARANCE_SELECTOR: NONE 1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
8
UIAlertView сustomization by subclassing
- (void)show - (void)drawRect:(CGRect)rect
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
9
Subclassing sample
• Simple • Keeps UIAlertView
behavior
• Fragile • Inflexible • Limited
WCAlertView: https://github.com/m1entus/WCAlertView
10
Custom UIView
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations • Status bar • showInView:
11
Custom UIView + keyWindow
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
• Status bar • show • Device rotation
12
Key Window Sample
• Status bar • Custom transitions • Flexible
• Rotation • Multiple views
presentation
JKProgressHUD: https://bitbucket.org/kluivers/jkprogresshud
13
UIView + UIWindow + UIViewController
1. Corners 2. Highlighted button color 3. Cancel button title color 4. Transition animations
• Status bar • show • Device rotation
14
Requisites
1. UIWindow + UIWindowLevelAlert 2. Root UIViewController = custom
container 3. Alert UIViewController 4. AlertView 5. Singleton = window controller 6. Duck typing
15
1. UIWindow + UIWindowLevelAlert
- (UIWindow *)window { if (_window == nil) { _window =
[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _window.windowLevel = UIWindowLevelAlert; _window.opaque = NO; _window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [_window setRootViewController:[self rootViewController]]; } return _window; }
Requisites
16
2. Root UIViewController - (MyAlertRootViewController *)rootViewController { if (_rootViewController == nil) { _rootViewController = [[MyAlertRootViewController alloc] init]; _rootViewController.delegate = self; } return _rootViewController; }
Requisites
17
3. Alert UIViewController 4. AlertView // MyAlertView - (void)show { [[MyAlertsController sharedInstance] showAlert:self animated:YES]; } - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex
animated:(BOOL)animated { [[MyAlertsController sharedInstance] dissmissAlert:self
animated:animated]; }
Requisites
18
5. Singleton (window controller)
- (void)showAlert:(UIView<MyTransitionDelegate> *)alertView animated:(BOOL)animated
{ [self.window setHidden:NO]; MyAlertViewController *viewController =
[[MyAlertViewController alloc] init]; viewController.alertView = alertView; [self.rootViewController displayViewController:viewController
animated:animated]; }
Requisites
19
6. Duck typing - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id<MyAlertViewDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION; - (void)show; - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated; - (NSInteger)addButtonWithTitle:(NSString *)title; - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
Requisites
20
Examples
21
Examples
22
Examples
23
Problems solved
• Corners MyAlertView.layer.cornerRadius
• Highlighted button color [UIButton setImage:forState:] • Cancel button title color setCancelButtonTitleColor: • Transition animations [MyAlertViewController showAnimated:completion] • Rotation Automatically by UIViewControllers
24
Advanced problems
• Multiple alerts simultaneously • Transition events • Interaction with system UIAlertView
25
Demo
Multiple alerts
26
Multiple alerts simultaneous display
• Alert displayed + show = dismiss + display
• Running transition + show/hide = add transition to wait queue
27
Transition events
1. Will Present 2. Did Present 3. Will Present 4. Did Present 5. Will Dismiss 6. Did Present 7. Did Dismiss 8. Will Dismiss 9. Did Dismiss
28
Interaction with UIAlertView
• UIWindowDidBecomeVisible • UIWindowDidBecomeHidden • UIWindowDidBecomeKey • UIWindowDidResignKey
• What should be done on receiving notifications?