mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.2 KiB
Matlab
133 lines
3.2 KiB
Matlab
8 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
|
|
||
|
#import "OWSProgressView.h"
|
||
4 years ago
|
#import <SessionUtilitiesKit/UIView+OWS.h>
|
||
|
#import <SessionUtilitiesKit/OWSMath.h>
|
||
8 years ago
|
|
||
8 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
8 years ago
|
@interface OWSProgressView ()
|
||
|
|
||
|
@property (nonatomic) CAShapeLayer *borderLayer;
|
||
|
@property (nonatomic) CAShapeLayer *progressLayer;
|
||
|
|
||
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
|
@implementation OWSProgressView
|
||
|
|
||
|
- (id)init
|
||
|
{
|
||
|
self = [super init];
|
||
|
if (self) {
|
||
|
[self initCommon];
|
||
|
}
|
||
|
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (id)initWithFrame:(CGRect)frame
|
||
|
{
|
||
|
self = [super initWithFrame:frame];
|
||
|
if (self) {
|
||
|
[self initCommon];
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (void)initCommon
|
||
|
{
|
||
|
self.opaque = NO;
|
||
|
self.userInteractionEnabled = NO;
|
||
|
self.color = [UIColor whiteColor];
|
||
|
|
||
7 years ago
|
// Prevent the shape layer from animating changes.
|
||
|
[CATransaction begin];
|
||
7 years ago
|
[CATransaction setDisableActions:YES];
|
||
7 years ago
|
|
||
8 years ago
|
self.borderLayer = [CAShapeLayer new];
|
||
|
[self.layer addSublayer:self.borderLayer];
|
||
|
|
||
|
self.progressLayer = [CAShapeLayer new];
|
||
|
[self.layer addSublayer:self.progressLayer];
|
||
|
|
||
7 years ago
|
[CATransaction commit];
|
||
|
|
||
8 years ago
|
[self setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
|
||
|
[self setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
|
||
|
}
|
||
|
|
||
|
- (void)layoutSubviews
|
||
|
{
|
||
|
[super layoutSubviews];
|
||
|
[self update];
|
||
|
}
|
||
|
|
||
|
- (void)setProgress:(CGFloat)progress
|
||
|
{
|
||
|
if (_progress != progress) {
|
||
|
_progress = progress;
|
||
|
[self update];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)setColor:(UIColor *)color
|
||
|
{
|
||
|
if (![_color isEqual:color]) {
|
||
|
_color = color;
|
||
|
[self update];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)update
|
||
|
{
|
||
7 years ago
|
// Prevent the shape layer from animating changes.
|
||
|
[CATransaction begin];
|
||
7 years ago
|
[CATransaction setDisableActions:YES];
|
||
7 years ago
|
|
||
7 years ago
|
CGFloat borderThickness = MAX(CGHairlineWidth(), self.bounds.size.height * 0.1f);
|
||
|
CGFloat cornerRadius = MIN(self.bounds.size.width, self.bounds.size.height) * 0.5f;
|
||
8 years ago
|
|
||
|
// Add the outer border.
|
||
7 years ago
|
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];
|
||
8 years ago
|
self.borderLayer.path = borderPath.CGPath;
|
||
7 years ago
|
self.borderLayer.strokeColor = self.color.CGColor;
|
||
|
self.borderLayer.lineWidth = borderThickness;
|
||
|
self.borderLayer.fillColor = [UIColor clearColor].CGColor;
|
||
8 years ago
|
|
||
|
// Add the inner progress.
|
||
7 years ago
|
CGRect progressRect = self.bounds;
|
||
|
progressRect.size.width = cornerRadius * 2;
|
||
|
CGFloat baseProgress = borderThickness * 2;
|
||
|
CGFloat minProgress = baseProgress;
|
||
|
CGFloat maxProgress = MAX(0, self.bounds.size.width - baseProgress);
|
||
6 years ago
|
progressRect.size.width = CGFloatLerp(minProgress, maxProgress, CGFloatClamp01(self.progress));
|
||
7 years ago
|
UIBezierPath *progressPath = [UIBezierPath bezierPathWithRoundedRect:progressRect cornerRadius:cornerRadius];
|
||
8 years ago
|
self.progressLayer.path = progressPath.CGPath;
|
||
|
self.progressLayer.fillColor = self.color.CGColor;
|
||
7 years ago
|
|
||
|
[CATransaction commit];
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
+ (CGSize)defaultSize
|
||
8 years ago
|
{
|
||
|
return CGSizeMake(150, 16);
|
||
|
}
|
||
|
|
||
7 years ago
|
- (CGSize)sizeThatFits:(CGSize)size
|
||
|
{
|
||
|
return OWSProgressView.defaultSize;
|
||
|
}
|
||
|
|
||
8 years ago
|
- (CGSize)intrinsicContentSize
|
||
|
{
|
||
|
return CGSizeMake(UIViewNoIntrinsicMetric, 16);
|
||
|
}
|
||
|
|
||
|
@end
|
||
8 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|