Skip to content

Commit

Permalink
Merge branch 'master' of github.com:SVProgressHUD/SVProgressHUD into …
Browse files Browse the repository at this point in the history
…toohotz/hudSpecificView
  • Loading branch information
toohotz committed May 5, 2017
2 parents 2490ffc + f7c8d0d commit 23ace16
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ Or show a confirmation glyph before before getting dismissed a little bit later.
+ (void)setErrorImage:(UIImage*)image; // default is bundled error image from Freepik
+ (void)setViewForExtension:(UIView*)view; // default is nil, only used if #define SV_APP_EXTENSIONS is set
+ (void)setMinimumDismissTimeInterval:(NSTimeInterval)interval; // default is 5.0 seconds
+ (void)setMaximumDismissTimeInterval:(NSTimeInterval)interval; // default is infinite
+ (void)setMaximumDismissTimeInterval:(NSTimeInterval)interval; // default is CGFLOAT_MAX
+ (void)setFadeInAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setFadeOutAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel; // default is UIWindowLevelNormal
+ (void)setHapticsEnabled:(BOOL)hapticsEnabled; // default is NO
```

Additionally `SVProgressHUD` supports the `UIAppearance` protocol for most of the above methods.
Expand All @@ -166,6 +167,18 @@ As standard `SVProgressHUD` offers two preconfigured styles:

If you want to use custom colors use `setForegroundColor` and `setBackgroundColor:`. These implicity set the HUD's style to `SVProgressHUDStyleCustom`.

## Haptic Feedback

For users with newer devices (starting with the iPhone 7), `SVProgressHUD` can automatically trigger haptic feedback depending on which HUD is being displayed. The feedback maps as follows:

* `showSuccessWithStatus:` <-> `UINotificationFeedbackTypeSuccess`
* `showInfoWithStatus:` <-> `UINotificationFeedbackTypeWarning`
* `showErrorWithStatus:` <-> `UINotificationFeedbackTypeError`

To enable this functionality, use `setHapticsEnabled:`.

Users with devices prior to iPhone 7 will have no change in functionality.

## Notifications

`SVProgressHUD` posts four notifications via `NSNotificationCenter` in response to being shown/dismissed:
Expand Down
5 changes: 4 additions & 1 deletion SVProgressHUD/SVProgressHUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ typedef void (^SVProgressHUDDismissCompletion)(void);
@property (strong, nonatomic) UIImage *errorImage UI_APPEARANCE_SELECTOR; // default is the bundled error image provided by Freepik
@property (strong, nonatomic) UIView *viewForExtension UI_APPEARANCE_SELECTOR; // default is nil, only used if #define SV_APP_EXTENSIONS is set
@property (assign, nonatomic) NSTimeInterval minimumDismissTimeInterval; // default is 5.0 seconds
@property (assign, nonatomic) NSTimeInterval maximumDismissTimeInterval; // default is infinite
@property (assign, nonatomic) NSTimeInterval maximumDismissTimeInterval; // default is CGFLOAT_MAX

@property (assign, nonatomic) UIOffset offsetFromCenter UI_APPEARANCE_SELECTOR; // default is 0, 0

Expand All @@ -76,6 +76,8 @@ typedef void (^SVProgressHUDDismissCompletion)(void);

@property (assign, nonatomic) UIWindowLevel maxSupportedWindowLevel; // default is UIWindowLevelNormal

@property (assign, nonatomic) BOOL hapticsEnabled; // default is NO

+ (void)setDefaultStyle:(SVProgressHUDStyle)style; // default is SVProgressHUDStyleLight
+ (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; // default is SVProgressHUDMaskTypeNone
+ (void)setDefaultAnimationType:(SVProgressHUDAnimationType)type; // default is SVProgressHUDAnimationTypeFlat
Expand All @@ -98,6 +100,7 @@ typedef void (^SVProgressHUDDismissCompletion)(void);
+ (void)setFadeInAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setFadeOutAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
+ (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel; // default is UIWindowLevelNormal
+ (void)setHapticsEnabled:(BOOL)hapticsEnabled; // default is NO

#pragma mark - Show Methods

Expand Down
64 changes: 62 additions & 2 deletions SVProgressHUD/SVProgressHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ @interface SVProgressHUD ()
@property (nonatomic, readonly) CGFloat visibleKeyboardHeight;
@property (nonatomic, readonly) UIWindow *frontWindow;

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
@property (nonatomic, strong) UINotificationFeedbackGenerator *hapticGenerator;
#endif

- (void)updateHUDFrame;

#if TARGET_OS_IOS
Expand Down Expand Up @@ -219,6 +223,9 @@ + (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel {
[self sharedView].maxSupportedWindowLevel = windowLevel;
}

+ (void)setHapticsEnabled:(BOOL)hapticsEnabled {
[self sharedView].hapticsEnabled = hapticsEnabled;
}

#pragma mark - Show Methods

Expand Down Expand Up @@ -284,6 +291,12 @@ + (void)showProgress:(float)progress status:(NSString*)status maskType:(SVProgre

+ (void)showInfoWithStatus:(NSString*)status {
[self showImage:[self sharedView].infoImage status:status];

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
dispatch_async(dispatch_get_main_queue(), ^{
[[self sharedView].hapticGenerator notificationOccurred:UINotificationFeedbackTypeWarning];
});
#endif
}

+ (void)showInfoWithStatus:(NSString *)status inView:(UIView *)view {
Expand All @@ -299,6 +312,12 @@ + (void)showInfoWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)mas

+ (void)showSuccessWithStatus:(NSString*)status {
[self showImage:[self sharedView].successImage status:status];

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
dispatch_async(dispatch_get_main_queue(), ^{
[[self sharedView].hapticGenerator notificationOccurred:UINotificationFeedbackTypeSuccess];
});
#endif
}

+ (void)showSuccessWithStatus:(NSString *)status inVIew:(UIView *)view {
Expand All @@ -310,10 +329,22 @@ + (void)showSuccessWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)
[self setDefaultMaskType:maskType];
[self showSuccessWithStatus:status];
[self setDefaultMaskType:existingMaskType];

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
dispatch_async(dispatch_get_main_queue(), ^{
[[self sharedView].hapticGenerator notificationOccurred:UINotificationFeedbackTypeSuccess];
});
#endif
}

+ (void)showErrorWithStatus:(NSString*)status {
[self showImage:[self sharedView].errorImage status:status];

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
dispatch_async(dispatch_get_main_queue(), ^{
[[self sharedView].hapticGenerator notificationOccurred:UINotificationFeedbackTypeError];
});
#endif
}

+ (void)showErrorWithStatus:(NSString *)status inVIew:(UIView *)view {
Expand All @@ -325,6 +356,12 @@ + (void)showErrorWithStatus:(NSString*)status maskType:(SVProgressHUDMaskType)ma
[self setDefaultMaskType:maskType];
[self showErrorWithStatus:status];
[self setDefaultMaskType:existingMaskType];

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
dispatch_async(dispatch_get_main_queue(), ^{
[[self sharedView].hapticGenerator notificationOccurred:UINotificationFeedbackTypeError];
});
#endif
}

+ (void)showImage:(UIImage*)image status:(NSString*)status {
Expand Down Expand Up @@ -437,6 +474,8 @@ - (instancetype)initWithFrame:(CGRect)frame {

_maxSupportedWindowLevel = UIWindowLevelNormal;

_hapticsEnabled = NO;

// Accessibility support
self.accessibilityIdentifier = @"SVProgressHUD";
self.accessibilityLabel = @"SVProgressHUD";
Expand Down Expand Up @@ -865,6 +904,11 @@ - (void)showProgress:(float)progress status:(NSString*)status {

// Show
[strongSelf showStatus:status];

// Tell the Haptics Generator to prepare for feedback, which may come soon
#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
[strongSelf.hapticGenerator prepare];
#endif
}
}];
}
Expand Down Expand Up @@ -1411,6 +1455,7 @@ -(UIView *)backgroundView {
}
}
default:
_backgroundView.backgroundColor = [UIColor clearColor];
break;
}

Expand Down Expand Up @@ -1544,8 +1589,9 @@ - (UIWindow *)frontWindow {
BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
BOOL windowIsVisible = !window.hidden && window.alpha > 0;
BOOL windowLevelSupported = (window.windowLevel >= UIWindowLevelNormal && window.windowLevel <= self.maxSupportedWindowLevel);

if(windowOnMainScreen && windowIsVisible && windowLevelSupported) {
BOOL windowKeyWindow = window.isKeyWindow;

if(windowOnMainScreen && windowIsVisible && windowLevelSupported && windowKeyWindow) {
return window;
}
}
Expand All @@ -1562,6 +1608,20 @@ - (void)addBlur
self.hudVibrancyView.effect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
}

#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
- (UINotificationFeedbackGenerator *)hapticGenerator {
// Only return if haptics are enabled
if(!self.hapticsEnabled) {
return nil;
}

if(!_hapticGenerator) {
_hapticGenerator = [[UINotificationFeedbackGenerator alloc] init];
}
return _hapticGenerator;
}
#endif


#pragma mark - UIAppearance Setters

Expand Down

0 comments on commit 23ace16

Please sign in to comment.