Last active
April 26, 2017 08:33
-
-
Save craigmarvelley/6ccb7933c66e83fc5c721f7e2bff3d4e to your computer and use it in GitHub Desktop.
NSProgress/UIProgressView example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a progress bar view | |
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar]; | |
progressView.progress = 0; | |
// Assume we have a progress object | |
NSProgress *progress = [self progressForOngoingTask]; | |
// Observe the 'fractionCompleted' property and update the progress view as it changes | |
[self.KVOController observe:progress keyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *, id> *_Nonnull change) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
progressView.progress = progress.fractionCompleted; | |
}); | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Observe a child NSProgress object's userInfo property and update the parent's userInfo as it changes | |
[self.KVOController observe:childProgress keyPath:@"userInfo" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, NSProgress * _Nonnull childProgress, NSDictionary<NSString *, id> *_Nonnull change) { | |
[parentProgress setUserInfoObject:childProgress.userInfo[@"notesDownloaded"] forKey:@"notesDownloaded"]; | |
[parentProgress setUserInfoObject:childProgress.userInfo[@"totalNotesToDownload"] forKey:@"totalNotesToDownload"]; | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment