Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
s2mh committed Oct 13, 2016
1 parent 0d71556 commit 4c8ab55
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 97 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Fonty-Demo/FYSelectFontTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@interface FYSelectFontTableViewCell : UITableViewCell

@property (nonatomic, assign) BOOL striped;
@property (nonatomic, assign) BOOL stripedPause;
@property (nonatomic, assign) BOOL pauseStripes;
@property (nonatomic, assign) double downloadProgress;

@end
118 changes: 63 additions & 55 deletions Fonty-Demo/FYSelectFontTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,50 @@

#import "FYSelectFontTableViewCell.h"

static const CGFloat StripeWidth = 20.0f;

@interface FYSelectFontTableViewCell ()

@property (nonatomic, strong) CAShapeLayer *downloadProgressLayer;
@property (nonatomic, strong) CALayer *stripesLayer;
@property (nonatomic, strong) CAShapeLayer *progressLayer;

@end

@implementation FYSelectFontTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
_striped = NO;
_stripedPause = NO;
_downloadProgressLayer = [CAShapeLayer layer];
_downloadProgressLayer.fillColor = [UIColor grayColor].CGColor;
_downloadProgressLayer.opacity = 0.5f;
[self.layer addSublayer:_downloadProgressLayer];
_pauseStripes = NO;
}
return self;
}

- (void)layoutSubviews {
[super layoutSubviews];

CGRect downloadProgressLayerFrame = self.bounds;
if (self.downloadProgress == 1.0f) {
[self.stripesLayer removeFromSuperlayer];
[self.progressLayer removeFromSuperlayer];
return;
}
if (self.striped) {
if (![self.stripesLayer animationForKey:@"position"]) {
[self animateStripes];
[self.progressLayer removeFromSuperlayer];
[self.layer addSublayer:self.stripesLayer];
if (self.pauseStripes) {
[self pauseLayer:self.stripesLayer];
} else {
if (self.stripedPause) {
[self pauseLayer:self.stripesLayer];
} else {
[self resumeLayer:self.stripesLayer];
}
[self resumeLayer:self.stripesLayer];
}
} else {
[self.stripesLayer removeFromSuperlayer];
CGFloat width = self.bounds.size.width;
downloadProgressLayerFrame.origin.x = width * self.downloadProgress;
downloadProgressLayerFrame.size.width = width * (1.0f - self.downloadProgress);
[self.layer addSublayer:self.progressLayer];
self.progressLayer.timeOffset = self.downloadProgress;
}

UIBezierPath *downloadProgressLayerPath = [UIBezierPath bezierPathWithRect:downloadProgressLayerFrame];
self.downloadProgressLayer.path = downloadProgressLayerPath.CGPath;
}

- (void)animateStripes {
CGFloat stripeWidth = self.bounds.size.height / 4.0f;

self.stripesLayer.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width + (4 * stripeWidth), self.bounds.size.height);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = 0.6f;
animation.repeatCount = HUGE_VALF;
animation.removedOnCompletion = NO;
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(- (2 * stripeWidth) + (self.bounds.size.width / 2.0f), self.bounds.size.height / 2.0f)];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(0 + (self.bounds.size.width / 2.0f), self.bounds.size.height / 2.0f)];
[self.stripesLayer addAnimation:animation forKey:@"position"];
[self.downloadProgressLayer addSublayer:self.stripesLayer];
}
#pragma mark - Private

- (void)pauseLayer:(CALayer *)layer {
if (layer.speed == 0.0f) {
Expand All @@ -96,30 +79,28 @@ - (void)resumeLayer:(CALayer *)layer {

- (CALayer *)stripesLayer {
if (!_stripesLayer) {
_stripesLayer = [CALayer layer];

CGFloat stripeWidth = self.bounds.size.height / 4.0f;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(stripeWidth * 4, stripeWidth * 4), NO, [UIScreen mainScreen].scale);
_stripesLayer = [CAShapeLayer layer];
_stripesLayer.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width + (4 * StripeWidth), self.bounds.size.height);
_stripesLayer.opacity = 0.5f;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(StripeWidth * 4, StripeWidth * 4), NO, [UIScreen mainScreen].scale);
//Fill the background
[[UIColor clearColor] setFill];
UIBezierPath *fillPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0f, 0.0f, stripeWidth * 4, stripeWidth * 4)];
UIBezierPath *fillPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0f, 0.0f, StripeWidth * 4, StripeWidth * 4)];
[fillPath fill];

//Draw the stripes
[[UIColor whiteColor] setFill];
[[UIColor grayColor] setFill];
for (int i = 0; i < 4; i++) {
//Create the four inital points of the fill shape
CGPoint bottomLeft = CGPointMake(-(stripeWidth * 4), stripeWidth * 4);
CGPoint topLeft = CGPointMake(0.0f, 0.0f);
CGPoint topRight = CGPointMake(stripeWidth, 0.0f);
CGPoint bottomRight = CGPointMake(-(stripeWidth * 4) + stripeWidth, stripeWidth * 4);
CGPoint bottomLeft = CGPointMake(-(StripeWidth * 4), StripeWidth * 4);
CGPoint topLeft = CGPointMake(0.0f, 0.0f);
CGPoint topRight = CGPointMake(StripeWidth, 0.0f);
CGPoint bottomRight = CGPointMake(-(StripeWidth * 4) + StripeWidth, StripeWidth * 4);
//Shift all four points as needed to draw all four stripes
bottomLeft.x += i * (2.0f * stripeWidth);
topLeft.x += i * (2.0f * stripeWidth);
topRight.x += i * (2.0f * stripeWidth);
bottomRight.x += i * (2.0f * stripeWidth);
bottomLeft.x += i * (2.0f * StripeWidth);
topLeft.x += i * (2.0f * StripeWidth);
topRight.x += i * (2.0f * StripeWidth);
bottomRight.x += i * (2.0f * StripeWidth);
//Create the fill path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:bottomLeft];
Expand All @@ -129,17 +110,44 @@ - (CALayer *)stripesLayer {
[path closePath];
[path fill];
}

//Retreive the image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Set the background of the progress layer
self.stripesLayer.backgroundColor = [UIColor colorWithPatternImage:image].CGColor;
_stripesLayer.backgroundColor = [UIColor colorWithPatternImage:image].CGColor;

CABasicAnimation *stripedAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
stripedAnimation.duration = 0.6f;
stripedAnimation.repeatCount = HUGE_VALF;
stripedAnimation.removedOnCompletion = NO;
stripedAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(- (2 * StripeWidth) + (self.bounds.size.width / 2.0f), self.bounds.size.height / 2.0f)];
stripedAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0 + (self.bounds.size.width / 2.0f), self.bounds.size.height / 2.0f)];

[_stripesLayer addAnimation:stripedAnimation forKey:@"stripedAnimation"];
}
return _stripesLayer;
}


- (CAShapeLayer *)progressLayer {
if (!_progressLayer) {
_progressLayer = [CAShapeLayer layer];
_progressLayer.fillColor = [UIColor grayColor].CGColor;
_progressLayer.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height);
_progressLayer.opacity = 0.5f;
_progressLayer.speed = 0.0f;

CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
progressAnimation.duration = 1.0f;
progressAnimation.removedOnCompletion = NO;
CGRect frame = self.bounds;
progressAnimation.fromValue = (__bridge id _Nullable)([UIBezierPath bezierPathWithRect:frame].CGPath);
frame.origin.x = self.bounds.size.width;
frame.size.width = 0.0f;
progressAnimation.toValue = (__bridge id _Nullable)([UIBezierPath bezierPathWithRect:frame].CGPath);

[_progressLayer addAnimation:progressAnimation forKey:@"progressAnimation"];
}
return _progressLayer;
}

@end
18 changes: 15 additions & 3 deletions Fonty-Demo/FYSelectFontViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
FYSelectFontTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FYSelectFontTableViewCell" forIndexPath:indexPath];

cell.textLabel.text = model.description;
cell.textLabel.font = [UIFont fy_fontWithURL:model.URL size:17.0f];
cell.detailTextLabel.text = nil;
cell.textLabel.font = [UIFont fy_fontWithURL:model.URL size:16.0f];

cell.downloadProgress = model.downloadProgress;
cell.striped = (model.status == FYFontModelDownloadStatusDownloaded) ? NO : model.fileSizeUnknown;
cell.stripedPause = (model.status == FYFontModelDownloadStatusSuspending);
cell.striped = NO;
cell.pauseStripes = NO;

if (model.status == FYFontModelDownloadStatusToBeDownloaded) {
if (model.downloadError) {
cell.detailTextLabel.text = model.downloadError.localizedDescription;
}
} else if (model.status == FYFontModelDownloadStatusDownloading) {
cell.striped = model.fileSizeUnknown;
} else if (model.status == FYFontModelDownloadStatusSuspending) {
cell.striped = model.fileSizeUnknown;
cell.pauseStripes = YES;
}

if (indexPath.row == self.fontManager.mainFontIndex) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Expand Down
4 changes: 2 additions & 2 deletions Fonty-Demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

[[FYFontManager sharedManager] setFontURLStringArray:@[@"http://115.28.28.235:8080/xx.ttf",
@"http://www.zhaozi.cn/e/enews/?enews=DownSoft&classid=297&id=22785&pathid=0&pass=6a9c20be7abab75c8128ada2c271b041&p=:::"]];
[[FYFontManager sharedManager] setFontURLStringArray:@[@"http://115.28.28.235:8088/SizeKnownFont.ttf",
@"http://115.28.28.235:8088/SizeUnknownFont.ttf"]];
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down
3 changes: 3 additions & 0 deletions Fonty/FYConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
extern NSString *const FYNewFontDownloadNotification;
extern NSString *const FYNewFontDownloadNotificationKey;


extern NSTimeInterval const FYNewFontDownloadTimeoutIntervalForRequest;

//extern const double FYNewFontFileSizeUnkownTag;

#endif /* FYConst_h */
2 changes: 1 addition & 1 deletion Fonty/FYConst.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
NSString *const FYNewFontDownloadNotification = @"FYNewFontDownloadNotification";
NSString *const FYNewFontDownloadNotificationKey = @"FYNewFontDownloadNotificationKey"; // FYDowloadFontModel

//const double FYNewFontFileSizeUnkownTag = -1.0f;
NSTimeInterval const FYNewFontDownloadTimeoutIntervalForRequest = 3 * 60.0f;
9 changes: 5 additions & 4 deletions Fonty/FYFontDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ - (void)downloadFontWithURL:(NSURL *)URL {

- (void)cancelDownloadingFontWithURL:(NSURL *)URL {
NSURLSessionDownloadTask *downloadTask = [self.taskDictionary objectForKey:URL];
if (downloadTask) {
// [self freeTask:downloadTask];
if (downloadTask && (downloadTask.state == NSURLSessionTaskStateRunning || downloadTask.state == NSURLSessionTaskStateSuspended)) {
[downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {}];
}
}

- (void)suspendDownloadWithURL:(NSURL *)URL {
NSURLSessionDownloadTask *downloadTask = [self.taskDictionary objectForKey:URL];
if (downloadTask) {
if (downloadTask && (downloadTask.state == NSURLSessionTaskStateRunning)) {
[downloadTask suspend];
}
}
Expand Down Expand Up @@ -100,7 +99,9 @@ - (void)freeTask:(NSURLSessionDownloadTask *)task {

- (NSURLSession *)session {
if (!_session) {
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = FYNewFontDownloadTimeoutIntervalForRequest;
_session = [NSURLSession sessionWithConfiguration:configuration
delegate:self
delegateQueue:nil];
}
Expand Down
26 changes: 9 additions & 17 deletions Fonty/FYFontManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ - (instancetype)init
{
self = [super init];
if (self) {
_fontCache = [FYFontCache sharedFontCache];
_fontCache = [FYFontCache sharedFontCache];
_fontDownloader = [FYFontDownloader sharedDownloader];
_fontRegister = [FYFontRegister sharedRegister];
_mainFontIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:FYMainFontIndexKey] integerValue];
_fontRegister = [FYFontRegister sharedRegister];
_mainFontIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:FYMainFontIndexKey] integerValue];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeModelStatus:)
selector:@selector(handleModelInNotification:)
name:FYNewFontDownloadNotification
object:nil];
}
Expand All @@ -65,7 +65,6 @@ - (UIFont *)fontWithURL:(NSURL *)URL size:(CGFloat)size {
if (![URL isKindOfClass:NSURL.class]) {
return [UIFont systemFontOfSize:size];
}

NSString *postScriptName = [self.postScriptNames objectForKey:URL.absoluteString];
UIFont *font = [UIFont fontWithName:postScriptName size:size];

Expand All @@ -85,13 +84,11 @@ - (UIFont *)fontWithURL:(NSURL *)URL size:(CGFloat)size {
}];
}
}

if (postScriptName) {
// found postScriptName
font = [UIFont fontWithName:postScriptName size:size];
}
}

return font;
}

Expand Down Expand Up @@ -158,19 +155,14 @@ - (void)pauseDownloadingWithURLString:(NSString *)URLString {

#pragma mark - Notification

- (void)changeModelStatus:(NSNotification *)NSNotification {
FYFontModel *downloadedModel = [NSNotification.userInfo objectForKey:FYNewFontDownloadNotificationKey];
- (void)handleModelInNotification:(NSNotification *)notification {
FYFontModel *newModel = [notification.userInfo objectForKey:FYNewFontDownloadNotificationKey];
for (FYFontModel *model in self.fontModelArray) {
if ([model.URL isEqual:downloadedModel.URL]) {
if (downloadedModel.status == FYFontModelDownloadStatusDownloading && !model.fileSizeUnknown && model.downloadProgress > downloadedModel.downloadProgress) {
break;
}
if (downloadedModel.status == FYFontModelDownloadStatusToBeDownloaded) {
if ([model.URL isEqual:newModel.URL]) {
[model setModel:newModel];
if (model.status == FYFontModelDownloadStatusToBeDownloaded) {
self.mainFontIndex = 0;
}
model.status = downloadedModel.status;
model.downloadProgress = downloadedModel.downloadProgress;
model.fileSizeUnknown = downloadedModel.fileSizeUnknown;
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Fonty/FYFontModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ typedef NS_ENUM(NSUInteger, FYFontModelDownloadStatus) {
@property (nonatomic, assign) double downloadProgress;
@property (nonatomic, copy) NSString *postScriptName;
@property (nonatomic, assign) BOOL fileSizeUnknown;
@property (nonatomic, copy) NSError *downloadError;

+ (instancetype)modelWithSessionDownloadTask:(NSURLSessionDownloadTask *)task;
- (void)setModel:(FYFontModel *)newModel;

@end
Loading

0 comments on commit 4c8ab55

Please sign in to comment.