Commit 6e0e758e cgx

实现倒计时圆弧进度

1 个父辈 85d6674e
正在显示 36 个修改的文件 包含 906 行增加69 行删除
//
// JXMovableCellTableView.h
// JXMovableCellTableView
//
// Created by jiaxin on 16/2/15.
// Copyright © 2016年 jiaxin. All rights reserved.
//
#import <UIKit/UIKit.h>
@class JXMovableCellTableView;
@protocol JXMovableCellTableViewDataSource <UITableViewDataSource>
@required
/**
* 获取tableView的数据源数组
*/
- (NSArray *)dataSourceArrayInTableView:(JXMovableCellTableView *)tableView;
/**
* 返回移动之后调换后的数据源
*/
- (void)tableView:(JXMovableCellTableView *)tableView newDataSourceArrayAfterMove:(NSArray *)newDataSourceArray;
@end
@protocol JXMovableCellTableViewDelegate <UITableViewDelegate>
@optional
/**
* 将要开始移动indexPath位置的cell
*/
- (void)tableView:(JXMovableCellTableView *)tableView willMoveCellAtIndexPath:(NSIndexPath *)indexPath;
/**
* 完成一次从fromIndexPath cell到toIndexPath cell的移动
*/
- (void)tableView:(JXMovableCellTableView *)tableView didMoveCellFromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
/**
* 结束移动cell在indexPath
*/
- (void)tableView:(JXMovableCellTableView *)tableView endMoveCellAtIndexPath:(NSIndexPath *)indexPath;
@end
@interface JXMovableCellTableView : UITableView
@property (nonatomic, weak) id<JXMovableCellTableViewDataSource> dataSource;
@property (nonatomic, weak) id<JXMovableCellTableViewDelegate> delegate;
/**
* 长按手势最小触发时间,默认1.0,最小0.2
*/
@property (nonatomic, assign) CGFloat gestureMinimumPressDuration;
/**
* 自定义可移动cell的截图样式
*/
@property (nonatomic, copy) void(^drawMovalbeCellBlock)(UIView *movableCell);
/**
* 是否允许拖动到屏幕边缘后,开启边缘滚动,默认YES
*/
@property (nonatomic, assign) BOOL canEdgeScroll;
/**
* 边缘滚动触发范围,默认150,越靠近边缘速度越快
*/
@property (nonatomic, assign) CGFloat edgeScrollRange;
@end
//
// ProgressCircleView.m
// DreamSleep
//
// Created by peter on 2022/7/13.
//
#import "ProgressCircleView.h"
@interface ProgressCircleView ()
@property (nonatomic, strong) UIImageView *dialplateIV;
@end
@implementation ProgressCircleView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self debugViewShowBorder];
self.backgroundColor = DSClearColor;
[self addSubview:self.dialplateIV];
[self.dialplateIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
make.width.height.equalTo(@170);
}];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGFloat radius = rect.size.height / 2.0;
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2.0;
UIColor *strokeColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? ColorFromHex(0xEEEEEE) : ColorFromHex(0x232A40);
[strokeColor setStroke];
[path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:0 endAngle:2*M_PI clockwise:YES];
[path stroke];
}
#pragma mark - lazy
- (UIImageView *)dialplateIV {
if (!_dialplateIV) {
_dialplateIV = [UIImageView new];
[_dialplateIV dk_setImagePicker:DKImagePickerWithNames(@"pic_zhunbei_biaopan", @"dk_pic_zhunbei_biaopan", @"pic_zhunbei_biaopan")];
}
return _dialplateIV;
}
@end
//
// ReadyItem.h
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// 睡前安排列表数据项
@interface ReadyItem : NSObject
/// 选项id
@property (nonatomic, assign) int item_id;
/// 白天背景
@property (nonatomic, copy) NSString *bg_day;
/// 晚上背景
@property (nonatomic, copy) NSString *bg_night;
/// 名称
@property (nonatomic, copy) NSString *item_name;
/// 时长(分钟)
@property (nonatomic, assign) int time;
/// 优选标记
@property (nonatomic, copy) NSString *flag_tips;
/// 选项描述
@property (nonatomic, copy) NSString *desc;
/// 选项图标
@property (nonatomic, copy) NSString *icon;
@end
NS_ASSUME_NONNULL_END
//
// ReadyItem.m
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "ReadyItem.h"
@implementation ReadyItem
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
return @{@"desc" : @"description"};
}
@end
//
// ReadyItemCell.h
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import <UIKit/UIKit.h>
#import "ReadyItem.h"
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, ItemCellType) {
ItemCellTypeStart, // 开始仪式itemcell
ItemCellTypeAdjust, // 设置睡前准备itemcell
};
/// 睡前安排列表顶部勾选任务cell
@interface ReadyItemCell : UIView
@property (nonatomic, strong) ReadyItem *item;
- (instancetype)initWithFrame:(CGRect)frame itemCellType:(ItemCellType)type;
@end
NS_ASSUME_NONNULL_END
//
// ReadyItemCell.m
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "ReadyItemCell.h"
@interface ReadyItemCell ()
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *nameLab;
@end
@implementation ReadyItemCell
- (instancetype)initWithFrame:(CGRect)frame itemCellType:(ItemCellType)type {
if (self = [super initWithFrame:frame]) {
[self addSubview:self.icon];
[self addSubview:self.timeLab];
if (type == ItemCellTypeStart) {
[self addSubview:self.nameLab];
}
[self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self);
make.width.height.equalTo(@40);
}];
[self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.left.right.equalTo(self);
}];
}
return self;
}
- (void)setItem:(ReadyItem *)item {
_item = item;
[self.icon yy_setImageWithURL:[NSURL URLWithString:item.icon] options:YYWebImageOptionShowNetworkActivity];
self.timeLab.text = [NSString stringWithFormat:@"%dmin", item.time];
}
#pragma mark - lazy
- (UIImageView *)icon {
if (!_icon) {
_icon = [UIImageView new];
}
return _icon;
}
- (UILabel *)timeLab {
if (!_timeLab) {
_timeLab = [UILabel labWithFont:SysFont(12.0)];
_timeLab.textAlignment = NSTextAlignmentCenter;
_timeLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHexA(0xFFFFFF, .3));
}
return _timeLab;
}
@end
......@@ -6,11 +6,11 @@
//
#import "ReadyItemView.h"
#import "ProgressCircleView.h"
#import "XLCircleProgress.h"
#import "RingingTools.h"
@interface ReadyItemView ()
@property (nonatomic, strong) ProgressCircleView *progressCircleView;
@property (nonatomic, strong) XLCircleProgress *circleProgress;
@property (nonatomic, strong) UIImageView *bgIV;
@property (nonatomic, strong) UIButton *enterBtn;
@end
......@@ -22,18 +22,13 @@
self.hidden = YES;
[self addSubview:self.bgIV];
[self addSubview:self.progressCircleView];
[self addSubview:self.circleProgress];
[self addSubview:self.enterBtn];
[self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.bottom.equalTo(self).offset(-34);
}];
[self.progressCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.width.height.equalTo(@200);
make.top.equalTo(self).offset(158);
}];
[self.enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(155, 40));
make.centerX.equalTo(self);
......@@ -46,14 +41,16 @@
- (void)start {
self.hidden = NO;
[RingingTools playRingingWithName:@"sleep_ready_transit"];
[self.circleProgress fire];
}
#pragma mark - lazy
- (ProgressCircleView *)progressCircleView {
if (!_progressCircleView) {
_progressCircleView = [ProgressCircleView new];
- (XLCircleProgress *)circleProgress {
if (!_circleProgress) {
_circleProgress = [[XLCircleProgress alloc] initWithFrame:CGRectMake(0, 158, 200, 200)];
_circleProgress.centerX = self.centerX;
}
return _progressCircleView;
return _circleProgress;
}
- (UIImageView *)bgIV {
......
......@@ -6,9 +6,15 @@
//
#import "ReadyListController.h"
#import "JXMovableCellTableView.h"
#import "SleepReadyRequestModel.h"
#import "RelaxItemsView.h"
@interface ReadyListController ()
@interface ReadyListController () <JXMovableCellTableViewDataSource, JXMovableCellTableViewDelegate>
@property (nonatomic, strong) RelaxItemsView *relaxItemsView;
@property (nonatomic, strong) JXMovableCellTableView *prepareItemsView;
@property (nonatomic, strong) NSArray *relax_items;
@property (nonatomic, strong) NSArray *prepare_items;
@end
@implementation ReadyListController
......@@ -25,12 +31,31 @@
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
self.naviTitle = @"设置睡前准备";
self.naviTitle = @"设置睡前安排";
self.naviBarAlpha = 1.0;
self.dsNaviBar.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, BrandColor);
[self.dsNaviBar addSubview:self.backBtn];
self.titleLab.dk_textColorPicker = DKColorPickerWithKey(Sub_Navi_TITLE);
[self.backBtn dk_setImage:DKImagePickerWithNames(@"sys_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
[self.view addSubview:self.relaxItemsView];
[self.view addSubview:self.prepareItemsView];
[self queryPreparePeaceListRequest];
}
- (void)queryPreparePeaceListRequest {
[SleepReadyRequestModel queryPreparePeaceListWithCompletion:^(SleepReadyRequestModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
[self.prepareItemsView.mj_header endRefreshing];
self.relax_items = requestModel.relax_items;
self.prepare_items = requestModel.prepare_items;
[self.relaxItemsView adjustRelax:self.relax_items];
[self.prepareItemsView reloadData];
}
}];
}
#pragma mark - 设置状态栏文字颜色
......@@ -38,4 +63,86 @@
return UIStatusBarStyleLightContent;
}
#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.prepare_items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"MoveCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
#pragma mark - JXMovableCellTableViewDataSource && JXMovableCellTableViewDelegate
- (NSArray *)dataSourceArrayInTableView:(JXMovableCellTableView *)tableView {
return self.prepare_items.copy;
}
- (void)tableView:(JXMovableCellTableView *)tableView newDataSourceArrayAfterMove:(NSArray *)newDataSourceArray {
self.prepare_items = newDataSourceArray.copy;
}
- (void)tableView:(JXMovableCellTableView *)tableView endMoveCellAtIndexPath:(NSIndexPath *)indexPath {
[self.prepareItemsView reloadData];
}
#pragma mark - lazy
- (RelaxItemsView *)relaxItemsView {
if (!_relaxItemsView) {
_relaxItemsView = [[RelaxItemsView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.dsNaviBar.frame) + 15, kScreenWidth - 30, 162)];
}
return _relaxItemsView;
}
- (JXMovableCellTableView *)prepareItemsView {
if (!_prepareItemsView) {
_prepareItemsView = [[JXMovableCellTableView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.relaxItemsView.frame) + 15, kScreenWidth - 30, kScreenHeight - self.dsNaviBar.height - self.relaxItemsView.height - 30 - 24 - Bottom_SafeArea_Height) style:UITableViewStylePlain];
_prepareItemsView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
[_prepareItemsView cornerRadius:24.0];
_prepareItemsView.showsVerticalScrollIndicator = NO;
_prepareItemsView.separatorStyle = UITableViewCellSeparatorStyleNone;
_prepareItemsView.dataSource = self;
_prepareItemsView.delegate = self;
_prepareItemsView.gestureMinimumPressDuration = 1.0;
_prepareItemsView.drawMovalbeCellBlock = ^(UIView *movableCell) {
movableCell.layer.shadowColor = [UIColor grayColor].CGColor;
movableCell.layer.masksToBounds = NO;
movableCell.layer.cornerRadius = 0;
movableCell.layer.shadowOffset = CGSizeMake(-5, 0);
movableCell.layer.shadowOpacity = 0.4;
movableCell.layer.shadowRadius = 5;
};
WS(weakSelf);
_prepareItemsView.mj_header = [DSGifHeader headerWithRefreshingBlock:^{
[weakSelf queryPreparePeaceListRequest];
}];
[_prepareItemsView.mj_header beginRefreshing];
}
return _prepareItemsView;
}
- (NSArray *)relax_items {
if (!_relax_items) {
_relax_items = [NSArray array];
}
return _relax_items;
}
- (NSArray *)prepare_items {
if (!_prepare_items) {
_prepare_items = [NSArray array];
}
return _prepare_items;
}
@end
//
// ProgressCircleView.h
// ReadyTimeMarker.h
// DreamSleep
//
// Created by peter on 2022/7/13.
// Created by peter on 2022/7/14.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/// 安睡准备时间进度表盘
@interface ProgressCircleView : UIView
/// 安睡准备时间调整控制器
@interface ReadyTimeMarker : UIView
@end
......
//
// ReadyTimeMarker.m
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "ReadyTimeMarker.h"
@interface ReadyTimeMarker () <UIGestureRecognizerDelegate>
@end
@implementation ReadyTimeMarker
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = DSClearColor;
UIImageView *markIV = [UIImageView new];
markIV.dk_imagePicker = DKImagePickerWithNames(@"ic_zhunbei_yidong", @"dk_ic_zhunbei_yidong", @"ic_zhunbei_yidong");
[self addSubview:markIV];
UIView *dotView = [UIView new];
[dotView cornerRadius:3.0];
dotView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xC7C7C7), ColorFromHex(0x383F55), DSWhite);
[self addSubview:dotView];
[markIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self);
make.width.height.equalTo(@30);
}];
[dotView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.bottom.equalTo(markIV.mas_top).offset(-29);
make.width.height.equalTo(@6);
}];
// 添加拖动手势
UIPanGestureRecognizer *panGesture = [UIPanGestureRecognizer new];
panGesture.delegate = self;
[self addGestureRecognizer:panGesture];
}
return self;
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
CGPoint locationPoint = [gestureRecognizer locationInView:gestureRecognizer.view];
if (translation.x < 0) {
// 向左滑
NSLog(@"向左滑");
} else if (translation.x > 0) {
// 向右滑
NSLog(@"向右滑");
}
return NO;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置线条的样式
CGContextSetLineCap(context, kCGLineCapRound);
// 绘制线的宽度
CGContextSetLineWidth(context, 1.0);
// 线的颜色
UIColor *strokeColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? ColorFromHex(0xC7C7C7) : ColorFromHex(0x383F55);
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
// 开始绘制
CGContextBeginPath(context);
// 设置虚线绘制起点
CGFloat x = self.width/2.0;
CGContextMoveToPoint(context, x, 0);
// lengths的值{10, 10}表示先绘制10个点,再跳过10个点,如此反复
CGFloat lengths[] = {4, 10};
// 虚线的起始点
CGContextSetLineDash(context, 0, lengths, 1);
// 绘制虚线的终点
CGContextAddLineToPoint(context, x, self.height - 30);
// 绘制
CGContextStrokePath(context);
// 关闭图像
CGContextClosePath(context);
}
@end
//
// RelaxItemsView.h
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface RelaxItemsView : UIView
- (void)adjustRelax:(NSArray *)relax_items;
@end
NS_ASSUME_NONNULL_END
//
// RelaxItemsView.m
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "RelaxItemsView.h"
#import "ReadyItemCell.h"
#import "ReadyTimeMarker.h"
@interface RelaxItemsView ()
@end
@implementation RelaxItemsView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
[self cornerRadius:24.0];
UIView *xAxis = [[UIView alloc] initWithFrame:CGRectMake(22, (self.height - 2)/2.0, self.width - 44, 2)];
[xAxis cornerRadius:1.0];
xAxis.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xDDDDDD), ColorFromHex(0x3B4258), DSWhite);
[self addSubview:xAxis];
}
return self;
}
- (void)adjustRelax:(NSArray *)relax_items {
CGFloat lineMargin = 22;
CGFloat lineW = self.width - 2*lineMargin;
CGFloat divideW = lineW / relax_items.count;
CGFloat itemW = 40;
CGFloat itemLeftMargin = lineMargin + (divideW - itemW) / 2.0;
for (int index = 0; index < relax_items.count; index++) {
ReadyItemCell *itemCell = [[ReadyItemCell alloc] initWithFrame:CGRectMake(itemLeftMargin + index * divideW, 24, itemW, self.height - 24 - 47) itemCellType:ItemCellTypeAdjust];
itemCell.item = relax_items[index];
[self addSubview:itemCell];
}
CGFloat markerW = 30;
CGFloat markerLeftMargin = lineMargin + divideW - markerW/2.0;
for (int index = 0; index < relax_items.count - 1; index++) {
ReadyTimeMarker *marker = [[ReadyTimeMarker alloc] initWithFrame:CGRectMake(markerLeftMargin + index * divideW, 24, markerW, self.height - 24 - 19)];
[self addSubview:marker];
}
}
@end
//
// SleepReadyRequestModel.h
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "DSNetworkTool.h"
NS_ASSUME_NONNULL_BEGIN
@interface SleepReadyRequestModel : DSNetworkTool
@property (nonatomic, strong) NSArray *relax_items;
@property (nonatomic, strong) NSArray *prepare_items;
/// 安睡准备设置页面列表数据
/// @param completion completion
+ (NSURLSessionDataTask *)queryPreparePeaceListWithCompletion:(void (^)(SleepReadyRequestModel *requestModel))completion;
@end
NS_ASSUME_NONNULL_END
//
// SleepReadyRequestModel.m
// DreamSleep
//
// Created by peter on 2022/7/14.
//
#import "SleepReadyRequestModel.h"
#import "ReadyItem.h"
@implementation SleepReadyRequestModel
+ (NSURLSessionDataTask *)queryPreparePeaceListWithCompletion:(void (^)(SleepReadyRequestModel *requestModel))completion {
SleepReadyRequestModel * requestModel = [[SleepReadyRequestModel alloc] init];
NSString *api = @"query_prepare_peace_list";
NSString *argStr = [NSString stringWithFormat:@"query{%@}", api];
return [self httpPostBodyRequestWithAPI:api params:@{@"query" : argStr} view:nil hasNetActivity:YES loadingInfo:nil hasFailInfo:YES success:^(NSDictionary * _Nonnull apiDic) {
DSLog(@"安睡准备设置页面列表数据接口apiDic:%@", apiDic);
requestModel.resCode = DSResCodeSuccess;
NSDictionary *resultDic = apiDic[@"result"];
NSArray *relax_items = resultDic[@"relax_items"];
NSArray *prepare_items = resultDic[@"prepare_items"];
NSMutableArray *tmp_relax_items = [NSMutableArray array];
for (int i = 0; i < relax_items.count; i++) {
ReadyItem *readyItem = [ReadyItem yy_modelWithJSON:relax_items[i]];
[tmp_relax_items addObject:readyItem];
}
requestModel.relax_items = [tmp_relax_items copy];
NSMutableArray *tmp_prepare_items = [NSMutableArray array];
for (int i = 0; i < prepare_items.count; i++) {
ReadyItem *prepareItem = [ReadyItem yy_modelWithJSON:prepare_items[i]];
[tmp_prepare_items addObject:prepareItem];
}
requestModel.prepare_items = [tmp_prepare_items copy];
completion(requestModel);
} failure:^(id _Nonnull failureInfo) {
requestModel.resCode = [failureInfo[@"errorCode"] integerValue];
requestModel.errMessage = failureInfo[@"errMessage"];
completion(requestModel);
}];
}
@end
//
// Circle.h
// YKL
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 安睡准备时间进度表盘
@interface XLCircle : UIView
- (instancetype)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth;
@property (assign,nonatomic) float progress;
@property (assign,nonatomic) CGFloat lineWidth;
@end
//
// Circle.m
// YKL
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
//
#import "XLCircle.h"
static CGFloat endPointMargin = 8.0f;
@interface XLCircle () {
CAShapeLayer* _trackLayer;
CAShapeLayer* _progressLayer;
// 进度指示器
UIImageView* _endPoint;
}
@end
@implementation XLCircle
- (instancetype)initWithFrame:(CGRect)frame lineWidth:(float)lineWidth {
if (self = [super initWithFrame:frame]) {
_lineWidth = lineWidth;
[self buildLayout];
}
return self;
}
- (void)buildLayout {
float centerX = self.bounds.size.width/2.0;
float centerY = self.bounds.size.height/2.0;
// 半径
float radius = (self.bounds.size.width - _lineWidth)/2.0;
// 创建贝塞尔路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
// 添加背景圆环
CAShapeLayer *backLayer = [CAShapeLayer layer];
backLayer.frame = self.bounds;
backLayer.fillColor = DSClearColor.CGColor;
UIColor *strokeColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? ColorFromHex(0xEEEEEE) : ColorFromHex(0x232A40);
backLayer.strokeColor = strokeColor.CGColor;
backLayer.lineWidth = _lineWidth;
backLayer.path = [path CGPath];
backLayer.strokeEnd = 1;
[self.layer addSublayer:backLayer];
// 创建进度layer
_progressLayer = [CAShapeLayer layer];
_progressLayer.frame = self.bounds;
_progressLayer.fillColor = DSClearColor.CGColor;
// 指定path的渲染颜色
_progressLayer.strokeColor = BrandColor.CGColor;
_progressLayer.lineCap = kCALineCapRound;
_progressLayer.lineWidth = _lineWidth;
_progressLayer.path = [path CGPath];
_progressLayer.strokeEnd = 0;
[self.layer addSublayer:_progressLayer];
// 设置渐变颜色
// CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// gradientLayer.frame = self.bounds;
// [gradientLayer setColors:[NSArray arrayWithObjects:(id)[RGB(255, 151, 0) CGColor],(id)[RGB(255, 203, 0) CGColor], nil]];
// gradientLayer.startPoint = CGPointMake(0, 0);
// gradientLayer.endPoint = CGPointMake(0, 1);
// // 用progressLayer来截取渐变层
// [gradientLayer setMask:_progressLayer];
// [self.layer addSublayer:gradientLayer];
// 进度指示器
_endPoint = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _lineWidth + endPointMargin*2, _lineWidth + endPointMargin*2)];
_endPoint.dk_imagePicker = DKImagePickerWithNames(@"ic_zhunbei_jindu", @"dk_ic_zhunbei_jindu", @"ic_zhunbei_jindu");
_endPoint.hidden = true;
_endPoint.backgroundColor = [UIColor greenColor];
_endPoint.layer.masksToBounds = true;
_endPoint.layer.cornerRadius = _endPoint.bounds.size.width/2;
[self addSubview:_endPoint];
}
- (void)setProgress:(float)progress {
_progress = progress;
_progressLayer.strokeEnd = progress;
[self updateEndPoint];
[_progressLayer removeAllAnimations];
}
// 更新小点的位置
- (void)updateEndPoint {
// 转成弧度
CGFloat angle = M_PI*2*_progress;
float radius = (self.bounds.size.width-_lineWidth)/2.0;
// 用户区分在第几象限内
int index = (angle)/M_PI_2;
// 用于计算正弦/余弦的角度
float needAngle = angle - index*M_PI_2;
float x = 0, y = 0;//用于保存_dotView的frame
switch (index) {
case 0: // 第一象限
x = radius + sinf(needAngle)*radius;
y = radius - cosf(needAngle)*radius;
break;
case 1: // 第二象限
x = radius + cosf(needAngle)*radius;
y = radius + sinf(needAngle)*radius;
break;
case 2: // 第三象限
x = radius - sinf(needAngle)*radius;
y = radius + cosf(needAngle)*radius;
break;
case 3: // 第四象限
x = radius - cosf(needAngle)*radius;
y = radius - sinf(needAngle)*radius;
break;
default:
break;
}
// 更新圆环的frame
CGRect rect = _endPoint.frame;
rect.origin.x = x - endPointMargin;
rect.origin.y = y - endPointMargin;
_endPoint.frame = rect;
// 移动到最前
[self bringSubviewToFront:_endPoint];
_endPoint.hidden = false;
if (_progress == 0 || _progress == 1) {
_endPoint.hidden = true;
}
}
@end
//
// CircleView.h
// YKL
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface XLCircleProgress : UIView
// 百分比
@property (assign,nonatomic) float progress;
- (void)fire;
@end
//
// CircleView.m
// YKL
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
//
#import "XLCircleProgress.h"
#import "XLCircle.h"
@implementation XLCircleProgress {
UIImageView *_dialplateIV;
XLCircle* _circle;
UILabel *_percentLabel;
NSTimer *_timer;
NSInteger _countTime;
NSInteger _totalTime;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initUI];
}
return self;
}
- (void)fire {
[_timer setFireDate:[NSDate date]];
}
- (void)dealloc {
[_timer invalidate];
_timer = nil;
}
- (void)initUI {
_countTime = 10*60;
_totalTime = _countTime;
float lineWidth = 2.0;
_circle = [[XLCircle alloc] initWithFrame:self.bounds lineWidth:lineWidth];
[self addSubview:_circle];
_dialplateIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
_dialplateIV.center = _circle.center;
[_dialplateIV dk_setImagePicker:DKImagePickerWithNames(@"pic_zhunbei_biaopan", @"dk_pic_zhunbei_biaopan", @"pic_zhunbei_biaopan")];
[self addSubview:_dialplateIV];
_percentLabel = [[UILabel alloc] initWithFrame:self.bounds];
_percentLabel.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, DkTitleColor, DSWhite);
_percentLabel.textAlignment = NSTextAlignmentCenter;
_percentLabel.font = BoldFont(34.0);
[self addSubview:_percentLabel];
WS(weakSelf);
_timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf dealTimerTask];
}];
[_timer setFireDate:[NSDate distantFuture]];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)dealTimerTask {
// 任务完成
if (_countTime == 0) {
[_timer setFireDate:[NSDate distantFuture]];
// 下一任务
return;
}
_countTime--;
NSString *countDownStr = [self formattimewithtimeinterval:_countTime];
_percentLabel.text = [NSString stringWithFormat:@"%@", countDownStr];
_circle.progress = (double)(_totalTime - _countTime)/(double)(_totalTime);
}
- (NSString *)formattimewithtimeinterval:(NSTimeInterval)timeinterval {
int minute = 0, hour = 0, secend = timeinterval;
minute = (secend % 3600)/60;
hour = secend / 3600;
secend = secend % 60;
if (hour == 0) {
return [NSString stringWithFormat:@"%02d:%02d", minute, secend];
} else if (minute == 0){
return [NSString stringWithFormat:@"%02d", secend];
} else {
return [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, secend];
}
}
#pragma mark - Setter
- (void)setProgress:(float)progress {
_progress = progress;
_circle.progress = progress;
}
@end
{
"images" : [
{
"filename" : "dk_ic_zhunbei_jindu.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "dk_ic_zhunbei_jindu@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "dk_ic_zhunbei_jindu@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "dk_ic_zhunbei_yidong.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "dk_ic_zhunbei_yidong@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "dk_ic_zhunbei_yidong@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "ic_zhunbei_jindu.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "ic_zhunbei_jindu@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "ic_zhunbei_jindu@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "ic_zhunbei_yidong.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "ic_zhunbei_yidong@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "ic_zhunbei_yidong@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!