我需要将UITapGestureRecognizer添加到SVProgressHUD。 SVProgressHUD已经可以使用-(void) dismiss;
关闭。此代码将基于秒关闭动画。
- (void)dismiss {
for (UIGestureRecognizer *gesture in [[[self class] sharedView] gestureRecognizers]) {
[[[self class] sharedView] removeGestureRecognizer:gesture];
}
NSDictionary *userInfo = [self notificationUserInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDWillDisappearNotification
object:nil
userInfo:userInfo];
self.activityCount = 0;
[UIView animateWithDuration:0.15
delay:0
options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 0.8, 0.8);
if(self.isClear) // handle iOS 7 UIToolbar not answer well to hierarchy opacity change
self.hudView.alpha = 0;
else
self.alpha = 0;
}
completion:^(BOOL finished){
if(self.alpha == 0 || self.hudView.alpha == 0) {
self.alpha = 0;
self.hudView.alpha = 0;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self cancelRingLayerAnimation];
[self addTapGestureToDismiss];
[_hudView removeFromSuperview];
_hudView = nil;
[_overlayView removeFromSuperview];
_overlayView = nil;
[_indefiniteAnimatedView removeFromSuperview];
_indefiniteAnimatedView = nil;
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
[[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDDidDisappearNotification
object:nil
userInfo:userInfo];
// Tell the rootViewController to update the StatusBar appearance
UIViewController *rootController = [[UIApplication sharedApplication] keyWindow].rootViewController;
if ([rootController respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[rootController setNeedsStatusBarAppearanceUpdate];
}
// uncomment to make sure UIWindow is gone from app.windows
//NSLog(@"%@", [UIApplication sharedApplication].windows);
//NSLog(@"keyWindow = %@", [UIApplication sharedApplication].keyWindow);
}
}];
}
我的想法是将tapGesture代码添加到dismiss方法。这是我到目前为止所写的。
- (void)addTapGestureToDismiss {
// Creation and initializer of the tap gesture
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(dismiss)];
// Specify that the gesture must be a single tap
tapRecognizer.numberOfTapsRequired = 1;
// Add the tap gesture recognizer to the view
[[[self class] sharedView] addGestureRecognizer:tapRecognizer];
}
如您所见,我只是在初始化tapGesture。我遇到了将其放置在几个地方并导致该应用程序只能一键点击的问题。在这个过程中,我感到非常困惑。我是不是该
最佳答案
基于Z.Hung的答案,您可以在SVProgressHUD上创建一个类别,因此您不必在使用它的每个视图控制器中重复此代码。
用法
只需导入此类别并致电[SVProgressHUD showDismissableErrorWithStatus:@"Error message here"];
代码
@interface SVProgressHUD (Dismissable)
+ (void)showDismissableErrorWithStatus:(NSString*)status;
@end
@implementation SVProgressHUD (Dismissable)
+ (void)showDismissableErrorWithStatus:(NSString*)status {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleHUDTappedNotification:) name:SVProgressHUDDidReceiveTouchEventNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleHUDDisappearedNotification:) name:SVProgressHUDWillDisappearNotification object:nil];
[SVProgressHUD showErrorWithStatus: status];
}
#pragma mark - NSNotificationCenter
+ (void)handleHUDTappedNotification: (NSNotification *)notification {
[SVProgressHUD dismiss];
}
+ (void)handleHUDDisappearedNotification: (NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDDidReceiveTouchEventNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDWillDisappearNotification object:nil];
}
@end
迅捷4
import SVProgressHUD
/// Ref: https://stackoverflow.com/a/41111242/425694
extension SVProgressHUD {
public static func showDismissableError(with status: String) {
let nc = NotificationCenter.default
nc.addObserver(
self, selector: #selector(hudTapped(_:)),
name: NSNotification.Name.SVProgressHUDDidReceiveTouchEvent,
object: nil
)
nc.addObserver(
self, selector: #selector(hudDisappeared(_:)),
name: NSNotification.Name.SVProgressHUDWillDisappear,
object: nil
)
SVProgressHUD.showError(withStatus: status)
SVProgressHUD.setDefaultMaskType(.clear)
}
@objc
private static func hudTapped(_ notification: Notification) {
SVProgressHUD.dismiss()
SVProgressHUD.setDefaultMaskType(.none)
}
@objc
private static func hudDisappeared(_ notification: Notification) {
let nc = NotificationCenter.default
nc.removeObserver(self, name: NSNotification.Name.SVProgressHUDDidReceiveTouchEvent, object: nil)
nc.removeObserver(self, name: NSNotification.Name.SVProgressHUDWillDisappear, object: nil)
SVProgressHUD.setDefaultMaskType(.none)
}
}