Created
January 26, 2021 16:06
-
-
Save Baw-Appie/1156bb7e41318d1d951055111e6da59c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// APToast.m | |
// APToast | |
// | |
// Created by 오지훈 on 2020/11/07. | |
// Copyright © 2020 오지훈. All rights reserved. | |
// | |
#import "APLoadingToast.h" | |
@implementation APLoadingToast | |
-(id)initWithView:(UIView *)rootView { | |
APLoadingToast *ret = [super init]; | |
if(ret) { | |
self.isVisible = false; | |
self.rootView = rootView; | |
self.toastView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TOAST_SIZE.width, TOAST_SIZE.height)]; | |
// self.toastView.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); | |
self.toastView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-self.toastView.frame.size.width/2, -50, TOAST_SIZE.width, TOAST_SIZE.height); | |
UIView *blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]]; | |
blurView.frame = CGRectMake(0, 0, TOAST_SIZE.width, TOAST_SIZE.height); | |
blurView.layer.cornerRadius = 25; | |
blurView.layer.masksToBounds = true; | |
[self.toastView addSubview:blurView]; | |
UIView *progressBar = [self addProgress]; | |
progressBar.frame = CGRectMake(25, self.toastView.frame.size.height/2, 50, 50); | |
[self.toastView addSubview:progressBar]; | |
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(42.5, 10, 115, 15)]; | |
self.titleLabel.textColor = [UIColor blackColor]; | |
self.titleLabel.font = [UIFont boldSystemFontOfSize:12]; | |
self.titleLabel.textAlignment = NSTextAlignmentCenter; | |
// self.titleLabel.text = @"Loading A-Bypass"; | |
[self.toastView addSubview:self.titleLabel]; | |
self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(42.5, 25, 115, 15)]; | |
self.subtitleLabel.textColor = [UIColor colorWithWhite:0 alpha:0.8]; | |
self.subtitleLabel.font = [UIFont systemFontOfSize:11]; | |
self.subtitleLabel.textAlignment = NSTextAlignmentCenter; | |
// self.subtitleLabel.text = @"4/8 Completed"; | |
[self.toastView addSubview:self.subtitleLabel]; | |
[self.rootView addSubview:self.toastView]; | |
} | |
return ret; | |
} | |
-(id)addProgress { | |
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; | |
CAShapeLayer *trackLayer = [CAShapeLayer layer]; | |
trackLayer.position = CGPointMake(0, 0); | |
trackLayer.lineCap = kCALineCapRound; | |
trackLayer.lineWidth = 2; | |
trackLayer.fillColor = [[UIColor clearColor] CGColor]; | |
trackLayer.strokeColor = [[UIColor colorWithWhite:0 alpha:0.3] CGColor]; | |
trackLayer.anchorPoint = CGPointMake(0.5, 0.5); | |
trackLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:10 startAngle:0 endAngle:2*M_PI clockwise:true].CGPath; | |
trackLayer.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0, 1); | |
self.shapeLayer = [CAShapeLayer layer]; | |
self.shapeLayer.position = CGPointMake(0, 0); | |
self.shapeLayer.lineCap = kCALineCapRound; | |
self.shapeLayer.lineWidth = 2; | |
self.shapeLayer.fillColor = [[UIColor clearColor] CGColor]; | |
self.shapeLayer.strokeColor = [[UIColor colorWithWhite:0 alpha:1] CGColor]; | |
self.shapeLayer.anchorPoint = CGPointMake(0.5, 0.5); | |
self.shapeLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:10 startAngle:0 endAngle:2*M_PI clockwise:true].CGPath; | |
self.shapeLayer.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0, 1); | |
self.shapeLayer.strokeEnd = 0; | |
[view.layer addSublayer:trackLayer]; | |
[view.layer addSublayer:self.shapeLayer]; | |
return view; | |
} | |
-(void)setPercent:(double)percent { | |
self.shapeLayer.strokeEnd = percent; | |
} | |
-(void)showToast:(NSString *)title withMessage:(NSString *)message { | |
if(self.isVisible) return; | |
self.isVisible = !self.isVisible; | |
self.titleLabel.text = title; | |
self.subtitleLabel.text = message; | |
self.toastView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-self.toastView.frame.size.width/2, -50, TOAST_SIZE.width, TOAST_SIZE.height); | |
[UIView animateWithDuration:0.3 animations:^() { | |
self.toastView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-self.toastView.frame.size.width/2, 40, TOAST_SIZE.width, TOAST_SIZE.height); | |
}]; | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
[self hideToast]; | |
}); | |
} | |
-(void)hideToast { | |
if(!self.isVisible) return; | |
self.isVisible = !self.isVisible; | |
self.toastView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-self.toastView.frame.size.width/2, 40, TOAST_SIZE.width, TOAST_SIZE.height); | |
[UIView animateWithDuration:0.3 animations:^() { | |
self.toastView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-self.toastView.frame.size.width/2, -50, TOAST_SIZE.width, TOAST_SIZE.height); | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment