-
Notifications
You must be signed in to change notification settings - Fork 1
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
Added Mod Med Animation #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// MMLogoAnimatedView.h | ||
// SVProgressHUD | ||
// | ||
// Created by Jing Wei Li on 8/9/18. | ||
// Copyright © 2018 Modernizing Medicine. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MMLogoAnimatedView : UIView | ||
|
||
@property (nonatomic, assign) CGFloat diameter; | ||
@property (nonatomic, assign) CFTimeInterval duration; | ||
@property (nonatomic, strong) UIColor *strokeColor; | ||
@property (nonatomic, assign) CGFloat offsetToCenter; | ||
|
||
- (void)layoutAnimatedLayer; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// | ||
// MMLogoAnimatedView.m | ||
// SVProgressHUD | ||
// | ||
// Created by Jing Wei Li on 8/9/18. | ||
// Copyright © 2018 Modernizing Medicine. All rights reserved. | ||
// | ||
|
||
#import "MMLogoAnimatedView.h" | ||
|
||
static NSString * const ringAnimationKey = @"ringAnimationKey"; | ||
static NSString * const heartbeatAnimationKey = @"heartbeatAnimationKey"; | ||
static CGFloat const ringStrokeWidth = 2.0; | ||
static CGFloat const heartbeatStrokeWidth = 5.0; | ||
|
||
@interface MMLogoAnimatedView() | ||
|
||
@property (nonatomic, strong) CAShapeLayer *ringLayer; | ||
@property (nonatomic, strong) CAShapeLayer *heartbeatLayer; | ||
|
||
@end | ||
|
||
@implementation MMLogoAnimatedView | ||
|
||
- (void)willMoveToSuperview:(UIView *)newSuperview { | ||
if (!newSuperview) { | ||
[self.ringLayer removeFromSuperlayer]; | ||
[self.heartbeatLayer removeFromSuperlayer]; | ||
self.ringLayer = nil; | ||
self.heartbeatLayer = nil; | ||
} | ||
} | ||
|
||
- (void)layoutAnimatedLayer { | ||
self.ringLayer = [self createRingLayer]; | ||
self.heartbeatLayer = [self createHeartbeatLayer]; | ||
[self.layer addSublayer:self.ringLayer]; | ||
[self.layer addSublayer:self.heartbeatLayer]; | ||
} | ||
|
||
- (void)setFrame:(CGRect)frame { | ||
if(!CGRectEqualToRect(frame, super.frame)) { | ||
[super setFrame:frame]; | ||
if(self.superview) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should do if (self.superview != nil) {
} |
||
self.offsetToCenter = self.diameter < self.superview.bounds.size.width ? (self.superview.bounds.size.width - self.diameter) / 2 : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can make this more debug friendly by doing: CGFloat superviewWidth = CGRectGetWidth(self.superview.bounds);
BOOL diameterLessThatSuperviewWidth = self.diameter < superviewWidth;
self.offsetToCenter = diameterLessThatSuperviewWidth ? (superviewWidth - self.diameter) / 2 : 0; |
||
[self layoutAnimatedLayer]; | ||
} | ||
} | ||
|
||
} | ||
|
||
- (CAShapeLayer *)createRingLayer { | ||
if (!self.ringLayer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
UIBezierPath *path = [[UIBezierPath alloc] init]; | ||
CGFloat centerLength = self.offsetToCenter + (self.diameter / 2.0); | ||
[path addArcWithCenter:CGPointMake(centerLength, centerLength) | ||
radius:self.diameter / 2 | ||
startAngle:0.09 * M_PI | ||
endAngle:2.09 * M_PI | ||
clockwise:YES]; | ||
|
||
self.ringLayer = [CAShapeLayer layer]; | ||
CAAnimationGroup *animationGroup = [self createReversibleAnimationOnLayer:self.ringLayer | ||
fromBezierPath:path | ||
strokeWidth:ringStrokeWidth]; | ||
|
||
[self.ringLayer addAnimation:animationGroup forKey:ringAnimationKey]; | ||
} | ||
|
||
return self.ringLayer; | ||
} | ||
|
||
- (CAShapeLayer *)createHeartbeatLayer { | ||
if (!self.heartbeatLayer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
UIBezierPath *path = [[UIBezierPath alloc] init]; | ||
[path moveToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.06, self.offsetToCenter + self.diameter * 0.69)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.25, self.offsetToCenter + self.diameter * 0.69)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.33, self.offsetToCenter + self.diameter * 0.41)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.46, self.offsetToCenter + self.diameter * 0.76)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.63, self.offsetToCenter + self.diameter * 0.24)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.73, self.offsetToCenter + self.diameter * 0.62)]; | ||
[path addLineToPoint:CGPointMake(self.offsetToCenter + self.diameter * 0.95, self.offsetToCenter + self.diameter * 0.62)]; | ||
|
||
self.heartbeatLayer = [CAShapeLayer layer]; | ||
CAAnimationGroup *animationGroup = [self createReversibleAnimationOnLayer:self.heartbeatLayer | ||
fromBezierPath:path | ||
strokeWidth:heartbeatStrokeWidth]; | ||
[self.heartbeatLayer addAnimation:animationGroup forKey:heartbeatAnimationKey]; | ||
} | ||
|
||
return self.heartbeatLayer; | ||
} | ||
|
||
- (CAAnimationGroup *)createReversibleAnimationOnLayer:(CAShapeLayer *)shapeLayer | ||
fromBezierPath:(UIBezierPath *)bezierPath | ||
strokeWidth:(CGFloat)strokeWidth { | ||
|
||
shapeLayer.path = bezierPath.CGPath; | ||
shapeLayer.fillColor = nil; | ||
shapeLayer.strokeColor = self.strokeColor.CGColor; | ||
shapeLayer.lineWidth = strokeWidth; | ||
shapeLayer.lineCap = kCALineCapRound; | ||
shapeLayer.lineJoin = kCALineJoinRound; | ||
|
||
CABasicAnimation *forwardAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; | ||
forwardAnimation.fromValue = @1.0; | ||
forwardAnimation.toValue = @0.0; | ||
forwardAnimation.duration = self.duration; | ||
forwardAnimation.beginTime = 0.0; | ||
|
||
CABasicAnimation *reverseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"]; | ||
reverseAnimation.fromValue = @1.0; | ||
reverseAnimation.toValue = @0.0; | ||
reverseAnimation.duration = self.duration; | ||
reverseAnimation.beginTime = self.duration; | ||
|
||
CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; | ||
animationGroup.animations = @[forwardAnimation, reverseAnimation]; | ||
animationGroup.repeatCount = INFINITY; | ||
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; | ||
animationGroup.autoreverses = YES; | ||
animationGroup.duration = self.duration * 2; | ||
|
||
return animationGroup; | ||
} | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should do