extract method for animations.

This commit is contained in:
Kyle Matthew Hedden 2020-09-21 17:41:59 -04:00
parent 4986bb09a9
commit cba21e3b44

View File

@ -298,58 +298,64 @@
__weak typeof(self) weakSelf = self;
MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) {
[MVMCoreDispatchUtility performBlockOnMainThread:^{
// Must notify animation delegate before animating.
if (animated && weakSelf.animationDelegate) {
[weakSelf.animationDelegate topAlertViewBeginAnimation];
}
[weakSelf.viewToLayout layoutIfNeeded];
weakSelf.topLabelConstraintBottom.active = NO;
weakSelf.topConstraint.active = YES;
weakSelf.expanded = YES;
void(^animation)(void) = ^(void) {
weakSelf.buttonView.button.alpha = 1;
weakSelf.buttonView.label.alpha = 1;
if (weakSelf.onlyShowTopMessageWhenCollapsed) {
weakSelf.shortViewHeight.active = YES;
}
[weakSelf.viewToLayout layoutIfNeeded];
};
//accessibility - added to make only top alert label and close button accessible. Posted notification when top alert is displayed
weakSelf.accessibilityElements = @[weakSelf.buttonView];
weakSelf.shortView.isAccessibilityElement = NO;
void(^completion)(void) = ^(void) {
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, weakSelf.buttonView.label);
[weakSelf performExpansion:animated onCompletion:^{
[operation markAsFinished];
};
if (animated) {
[UIView animateWithDuration:.5 animations:animation completion:^(BOOL finished) {
[weakSelf.viewToLayout layoutIfNeeded];
// Must notify animation delegate when animating finished.
[MVMCoreDispatchUtility performBlockInBackground:^{
if (weakSelf.animationDelegate) {
[weakSelf.animationDelegate topAlertViewFinishAnimation];
}
}];
completion();
}];
} else {
animation();
completion();
}
// Collapse after 5 seconds (if the view still exists)
[weakSelf autoCollapse];
}];
}];
}];
[[MVMCoreNavigationHandler sharedNavigationHandler] addNavigationOperation:operation];
}
}
- (void)performExpansion:(BOOL)animated onCompletion:(void(^)(void))completionHandler {
// Must notify animation delegate before animating.
if (animated && self.animationDelegate) {
[self.animationDelegate topAlertViewBeginAnimation];
}
[self.viewToLayout layoutIfNeeded];
self.topLabelConstraintBottom.active = NO;
self.topConstraint.active = YES;
self.expanded = YES;
void(^animation)(void) = ^(void) {
self.buttonView.button.alpha = 1;
self.buttonView.label.alpha = 1;
if (self.onlyShowTopMessageWhenCollapsed) {
self.shortViewHeight.active = YES;
}
[self.viewToLayout layoutIfNeeded];
};
//accessibility - added to make only top alert label and close button accessible. Posted notification when top alert is displayed
self.accessibilityElements = @[self.buttonView];
self.shortView.isAccessibilityElement = NO;
void(^completion)(void) = ^(void) {
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self.buttonView.label);
completionHandler();
};
if (animated) {
[UIView animateWithDuration:.5 animations:animation completion:^(BOOL finished) {
[self.viewToLayout layoutIfNeeded];
// Must notify animation delegate when animating finished.
[MVMCoreDispatchUtility performBlockInBackground:^{
if (self.animationDelegate) {
[self.animationDelegate topAlertViewFinishAnimation];
}
}];
completion();
}];
} else {
animation();
completion();
}
// Collapse after 5 seconds (if the view still exists)
[self autoCollapse];
}
- (void)autoCollapse {
if (self.collapseAutomaticallyAfterExpanded) {
__weak typeof(self) weakSelf = self;
@ -379,34 +385,8 @@
__weak typeof(self) weakSelf = self;
MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) {
[MVMCoreDispatchUtility performBlockOnMainThread:^{
// Must notify animation delegate before animating.
if (weakSelf.animationDelegate) {
[weakSelf.animationDelegate topAlertViewBeginAnimation];
}
[weakSelf.viewToLayout layoutIfNeeded];
weakSelf.topConstraint.active = NO;
weakSelf.topLabelConstraintBottom.active = YES;
weakSelf.expanded = NO;
[UIView animateWithDuration:.5 animations:^{
[weakSelf.viewToLayout layoutIfNeeded];
weakSelf.buttonView.button.alpha = 0;
weakSelf.buttonView.label.alpha = 0;
weakSelf.shortViewHeight.active = NO;
} completion:^(BOOL finished) {
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[strongSelf.viewToLayout layoutIfNeeded];
strongSelf.accessibilityElements = @[strongSelf.shortView.label];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
[MVMCoreDispatchUtility performBlockInBackground:^{
// Must notify animation delegate when animating finished.
if (weakSelf.animationDelegate) {
[weakSelf.animationDelegate topAlertViewFinishAnimation];
}
[operation markAsFinished];
}];
[weakSelf performCollapseAnimationThen:^{
[operation markAsFinished];
}];
}];
}];
@ -414,6 +394,34 @@
}
}
- (void)performCollapseAnimationThen:(void(^)(void))completionHandler {
// Must notify animation delegate before animating.
if (self.animationDelegate) {
[self.animationDelegate topAlertViewBeginAnimation];
}
[self.viewToLayout layoutIfNeeded];
self.topConstraint.active = NO;
self.topLabelConstraintBottom.active = YES;
self.expanded = NO;
[UIView animateWithDuration:.5 animations:^{
[self.viewToLayout layoutIfNeeded];
self.buttonView.button.alpha = 0;
self.buttonView.label.alpha = 0;
self.shortViewHeight.active = NO;
} completion:^(BOOL finished) {
[self.viewToLayout layoutIfNeeded];
self.accessibilityElements = @[self.shortView.label];
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
// Must notify animation delegate when animating finished.
[MVMCoreDispatchUtility performBlockInBackground:^{
if (self.animationDelegate) {
[self.animationDelegate topAlertViewFinishAnimation];
}
}];
completionHandler();
}];
}
- (void)accessibilityFocusChanged:(NSNotification *)notification {
if (![MVMCoreUIUtility viewContainsAccessiblityFocus:self]) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIAccessibilityElementFocusedNotification object:nil];