2

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 を初期化しています。いくつかの場所に配置すると、アプリのタップが 1 回だけになるという問題が発生しました。私はその過程でかなり混乱しました。するべきか

  • このジェスチャーをビューに追加しますか?
  • このジェスチャーを追加して却下しますか?
4

5 に答える 5

2

アップデート 2.0

しばらくして、このソリューションに出くわし、この質問を思い出しました。テストした限りでは機能します。ViewController クラスの viewWillAppear に Observer を追加するだけです。以前の回答のようにライブラリを変更する必要はありません。

-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tapToDismiss:) name:SVProgressHUDDidReceiveTouchEventNotification object:nil];
    //Other initializing
}
-(void)tapToDismiss:(NSNotification *)notification{
    [SVProgressHUD dismiss];
    //maybe other code to stop whatever your progress is
}

masktype がある場合でも、これにより SVProgressHUD が閉じられるはずです。

これを使用して、完了後に Observer を削除します (viewDidDisappear のように)。そうしないと、アプリの存続期間中ずっとそこに残ります。

[[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDDidReceiveTouchEventNotification object:nil];

クレジット: http://kevsaidwhat.blogspot.my/2013/06/cancel-svprogresshud-process-by-tapping.html

于 2016-09-01T06:09:07.593 に答える
0

@charmingToadに感謝します。ここで回答があれば、もう少し最適化された機能的なソリューションを上に投稿したいと思います。

私の場合、ユーザーがタップしてキャンセルした場合に操作をキャンセルするには、却下を知る必要があります。

import SVProgressHUD

extension SVProgressHUD {
    private static var dismissCompletion: (() -> ())?
    
    public static func showDismissable(with status: String, tapDismissed: (() -> ())? = nil) {
        dismissCompletion = tapDismissed
        
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(
            self, selector: #selector(hudTapped(_:)),
            name: NSNotification.Name.SVProgressHUDDidReceiveTouchEvent,
            object: nil
        )
        notificationCenter.addObserver(
            self, selector: #selector(hudDisappeared(_:)),
            name: NSNotification.Name.SVProgressHUDWillDisappear,
            object: nil
        )
        SVProgressHUD.show(withStatus: status)
        SVProgressHUD.setDefaultMaskType(.black)
    }

  @objc
  private static func hudTapped(_ notification: Notification) {
    SVProgressHUD.dismiss()
    SVProgressHUD.setDefaultMaskType(.none)
    dismissCompletion?()
  }

  @objc
  private static func hudDisappeared(_ notification: Notification) {
    dismissCompletion = nil
    let notificationCenter = NotificationCenter.default
    notificationCenter.removeObserver(self, name: NSNotification.Name.SVProgressHUDDidReceiveTouchEvent, object: nil)
    notificationCenter.removeObserver(self, name: NSNotification.Name.SVProgressHUDWillDisappear, object: nil)
    SVProgressHUD.setDefaultMaskType(.none)
  }
}
于 2020-07-18T20:10:04.080 に答える