所以,也感谢@trojanfoe 的帮助,我做到了这一点。
基本上,我在创建警报时没有设置警报持续时间,而是根据下载进度更新它。在下载完成之前,我反复将持续时间设置为 20.0f 。然后,当下载完成时,我将持续时间设置为 1.0f(因此警报将在 1 秒内消失)。
NSTimeInterval _alertDuration;
- (void)viewDidLoad {
[request1 conditionallyBeginAccessingResourcesWithCompletionHandler:^
(BOOL resourcesAvailable)
{
if (resourcesAvailable) {
// use it
} else {
[request1 beginAccessingResourcesWithCompletionHandler:^
(NSError * _Nullable error)
{
if (error == nil) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[alertDownload showCustom:self image:[UIImage
imageNamed:@"icon.jpg"]
color:[UIColor blueColor]
title:@"Download..."
subTitle:@"Download in progress"
closeButtonTitle:nil
duration:_alertDuration];
}
];
} else {
// handle error
}
}];
}
}];
.
- (void)observeValueForKeyPath:(nullable NSString *)keyPath
ofObject:(nullable id)object
change:(nullable NSDictionary *)change
context:(nullable void *)context {
if((object == request1.progress) && [keyPath
isEqualToString:@"fractionCompleted"]) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
if(request1.progress.fractionCompleted == 1) {
_alertDuration = 1.0f;
} else {
_alertDuration = 20.0f;
}
}];
}
}