MBProgressHUD或DSActivityView-点按即可取消

问题描述 投票:1回答:3

我同时遇到了MBProgressHUD和DSActivityView,以在iPhone应用程序上显示黑色的圆角“正在加载”类型的叠加层。

我只是想知道是否有人知道如何扩展这两种方法中的任何一种来检测覆盖的轻敲,以便可以取消操作。

我已经看到至少有一个带有“正在加载”指示器的应用。但是带有文本“点击取消”。

谢谢!

iphone xcode ios sdk uiprogressview
3个回答
27
投票
- (void)showHUDWithCancel:(NSString *)aMessage {
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = aMessage;
    self.HUD.detailsLabelText = @"Tap to cancel";
    [self.HUD addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hudWasCancelled)]];
}

- (void)hudWasCancelled {
    [self.HUD hide:YES];
}

1
投票

[最简单的方法是向相关视图添加手势识别器(单击)(对于MBProgressHUD,这可以是类本身,因为MBProgressHUDUIView的子类)) 。检测到敲击后,可以触发解除方法([MBProgress HUD hide])。

您可能还希望触发某种类型的NSNotification以便您的应用启动,因为大概除了删除加载视图本身之外,您还希望取消并清理在加载。


0
投票

注意:对于那些使用最新版本的MBProgressView的用户,按钮文档已更改:

/**
 * A button that is placed below the labels. Visible only if a target / action is added and a title is assigned..
 */

因此,创建的内容应类似于以下内容:

class Tools {

  static func popLoadingDialog(viewParent: UIView,
                                 label: String,
                                 cancelTarget: Any? = nil,
                                 cancelSelector: Selector? = nil) -> MBProgressHUD {

        let loadingNotification = MBProgressHUD.showAdded(to: viewParent, animated: true)
        loadingNotification.mode = MBProgressHUDMode.indeterminate
        loadingNotification.label.text = label
        if(cancelSelector != nil) {
            loadingNotification.button.setTitle("Cancel", for: .normal)
            loadingNotification.button.addTarget(cancelTarget, action: cancelSelector!, for: .touchUpInside)
        }
        return loadingNotification
    }
}

并称呼它:

loadingIndicator = Tools.createLoadingDialog(viewParent: view,
                                                      label: "Please wait...",
                                                      cancelTarget: self,
                                                      cancelSelector: #selector(onCancelClick))
loadingIndicator?.show(animated: true)

}

@objc func onCancelClick(){
        // do something when the user click on cancel...
}
© www.soinside.com 2019 - 2024. All rights reserved.