Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ProgressView] Backward animation support #1138

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified components/ProgressView/docs/assets/progress_view.mp4
Binary file not shown.
Binary file modified components/ProgressView/docs/assets/progress_view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 57 additions & 21 deletions components/ProgressView/examples/ProgressViewExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ @interface ProgressViewExample : UIViewController
@property(nonatomic, strong) MDCProgressView *fullyColoredProgressView;
@property(nonatomic, strong) UILabel *fullyColoredProgressLabel;

@property(nonatomic, strong) MDCProgressView *backwardProgressView;
@property(nonatomic, strong) UILabel *backwardProgressLabel;
@property(nonatomic, strong) MDCProgressView *backwardProgressResetView;
@property(nonatomic, strong) UILabel *backwardProgressResetLabel;

@property(nonatomic, strong) MDCProgressView *backwardProgressAnimateView;
@property(nonatomic, strong) UILabel *backwardProgressAnimateLabel;

@end

Expand Down Expand Up @@ -64,11 +67,18 @@ - (void)setupProgressViews {
// Hide the progress view at setup time.
_fullyColoredProgressView.hidden = YES;

_backwardProgressView = [[MDCProgressView alloc] init];
_backwardProgressView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_backwardProgressView];
_backwardProgressResetView = [[MDCProgressView alloc] init];
_backwardProgressResetView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_backwardProgressResetView];
// Have a non-zero progress at setup time.
_backwardProgressResetView.progress = 0.33;

_backwardProgressAnimateView = [[MDCProgressView alloc] init];
_backwardProgressAnimateView.translatesAutoresizingMaskIntoConstraints = NO;
_backwardProgressAnimateView.backwardProgressAnimationMode = MDCProgressViewBackwardAnimationModeAnimate;
[self.view addSubview:_backwardProgressAnimateView];
// Have a non-zero progress at setup time.
_backwardProgressView.progress = 0.33;
_backwardProgressAnimateView.progress = 0.33;
}

@end
Expand Down Expand Up @@ -111,12 +121,19 @@ - (void)setupLabels {
_fullyColoredProgressLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_fullyColoredProgressLabel];

_backwardProgressLabel = [[UILabel alloc] init];
_backwardProgressLabel.text = @"Backward progress";
_backwardProgressLabel.font = [MDCTypography captionFont];
_backwardProgressLabel.alpha = [MDCTypography captionFontOpacity];
_backwardProgressLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_backwardProgressLabel];
_backwardProgressResetLabel = [[UILabel alloc] init];
_backwardProgressResetLabel.text = @"Backward progress (reset)";
_backwardProgressResetLabel.font = [MDCTypography captionFont];
_backwardProgressResetLabel.alpha = [MDCTypography captionFontOpacity];
_backwardProgressResetLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_backwardProgressResetLabel];

_backwardProgressAnimateLabel = [[UILabel alloc] init];
_backwardProgressAnimateLabel.text = @"Backward progress (animate)";
_backwardProgressAnimateLabel.font = [MDCTypography captionFont];
_backwardProgressAnimateLabel.alpha = [MDCTypography captionFontOpacity];
_backwardProgressAnimateLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_backwardProgressAnimateLabel];
}

- (void)setupConstraints {
Expand All @@ -127,8 +144,10 @@ - (void)setupConstraints {
@"tintedLabel" : _tintedProgressLabel,
@"coloredView" : _fullyColoredProgressView,
@"coloredLabel" : _fullyColoredProgressLabel,
@"backwardView" : _backwardProgressView,
@"backwardLabel" : _backwardProgressLabel,
@"backwardResetView" : _backwardProgressResetView,
@"backwardResetLabel" : _backwardProgressResetLabel,
@"backwardAnimateView" : _backwardProgressAnimateView,
@"backwardAnimateLabel" : _backwardProgressAnimateLabel,
};
NSDictionary *metrics = @{
@"p" : @20,
Expand All @@ -141,7 +160,8 @@ - (void)setupConstraints {
"[stockView(==h)]-(p)-[stockLabel]-(s)-"
"[tintedView(==h)]-(p)-[tintedLabel]-(s)-"
"[coloredView(==h)]-(p)-[coloredLabel]-(s)-"
"[backwardView(==h)]-(p)-[backwardLabel]"
"[backwardResetView(==h)]-(p)-[backwardResetLabel]-(s)-"
"[backwardAnimateView(==h)]-(p)-[backwardAnimateLabel]"
options:0
metrics:metrics
views:views];
Expand All @@ -152,11 +172,13 @@ - (void)setupConstraints {
@"H:|-(p)-[stockView]-(p)-|",
@"H:|-(p)-[tintedView]-(p)-|",
@"H:|-(p)-[coloredView]-(p)-|",
@"H:|-(p)-[backwardView]-(p)-|",
@"H:|-(p)-[backwardResetView]-(p)-|",
@"H:|-(p)-[backwardAnimateView]-(p)-|",
@"H:|-(>=p)-[stockLabel]-(>=p)-|",
@"H:|-(>=p)-[tintedLabel]-(>=p)-|",
@"H:|-(>=p)-[coloredLabel]-(>=p)-|",
@"H:|-(>=p)-[backwardLabel]-(>=p)-|",
@"H:|-(>=p)-[backwardResetLabel]-(>=p)-|",
@"H:|-(>=p)-[backwardAnimateLabel]-(>=p)-|",
];
for (NSString *format in horizontalVisualFormats) {
[horizontalConstraints
Expand All @@ -173,7 +195,8 @@ - (void)viewDidAppear:(BOOL)animated {
[self animateStep1:_stockProgressView];
[self animateStep1:_tintedProgressView];
[self animateStep1:_fullyColoredProgressView];
[self animateBackwardProgressView];
[self animateBackwardProgressResetView];
[self animateBackwardProgressAnimateView];
}

- (void)animateStep1:(MDCProgressView *)progressView {
Expand Down Expand Up @@ -213,17 +236,30 @@ - (void)animateStep4:(MDCProgressView *)progressView {
}];
}

- (void)animateBackwardProgressView {
- (void)animateBackwardProgressResetView {
__weak ProgressViewExample *weakSelf = self;
[_backwardProgressView setProgress:1 - _backwardProgressView.progress

[_backwardProgressResetView setProgress:1 - _backwardProgressResetView.progress
animated:YES
completion:^(BOOL finished) {
[weakSelf performSelector:@selector(animateBackwardProgressView)
[weakSelf performSelector:@selector(animateBackwardProgressResetView)
withObject:nil
afterDelay:MDCProgressViewAnimationDuration];
}];
}

- (void)animateBackwardProgressAnimateView {
__weak ProgressViewExample *weakSelf = self;

[_backwardProgressAnimateView setProgress:1 - _backwardProgressResetView.progress
animated:YES
completion:^(BOOL finished) {
[weakSelf performSelector:@selector(animateBackwardProgressAnimateView)
withObject:nil
afterDelay:MDCProgressViewAnimationDuration];
}];
}

#pragma mark - Catalog by Convention

+ (NSArray *)catalogBreadcrumbs {
Expand Down
18 changes: 18 additions & 0 deletions components/ProgressView/src/MDCProgressView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

#import <UIKit/UIKit.h>

/** The animation mode when animating backward progress. */
typedef NS_ENUM(NSInteger, MDCProgressViewBackwardAnimationMode) {

/** Animate negative progress by resetting to 0 and then animating to the new value. */
MDCProgressViewBackwardAnimationModeReset,

/** Animate negative progress by animating from the current value. */
MDCProgressViewBackwardAnimationModeAnimate
};

/**
A Material linear determinate progress view.

Expand Down Expand Up @@ -48,6 +58,14 @@ IB_DESIGNABLE
*/
@property(nonatomic, assign) float progress;

/**
The backward progress animation mode.

When animating progress which is lower than the current progress value, this mode
will determine which animation to use. The default is MDCProgressViewBackwardAnimationModeReset.
*/
@property(nonatomic, assign) MDCProgressViewBackwardAnimationMode backwardProgressAnimationMode;

/**
Adjusts the current progress, optionally animating the change.

Expand Down
7 changes: 4 additions & 3 deletions components/ProgressView/src/MDCProgressView.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ - (void)commonMDCProgressViewInit {
self.clipsToBounds = YES;
self.isAccessibilityElement = YES;

_backwardProgressAnimationMode = MDCProgressViewBackwardAnimationModeReset;

_trackView = [[UIView alloc] initWithFrame:self.frame];
_trackView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:_trackView];
Expand Down Expand Up @@ -132,12 +134,11 @@ - (void)setProgress:(float)progress {
- (void)setProgress:(float)progress
animated:(BOOL)animated
completion:(void (^__nullable)(BOOL finished))completion {
// Backward progress should not be animated between the old and new progress. Instead, reset to 0
// without animation before animating to the new position.
if (progress < self.progress) {
if (progress < self.progress && self.backwardProgressAnimationMode == MDCProgressViewBackwardAnimationModeReset) {
self.progress = 0;
[self updateProgressView];
}

self.progress = progress;
[UIView animateWithDuration:animated ? [[self class] animationDuration] : 0
delay:0
Expand Down