时间轴的制作

制作时间轴时最重要的是计算下一个圆点及时间轴的长度

1.计算高度可以按取出的当前模型的内容计算,如下:

//取出的每个模型
 GTimeLineViewModel *timeLimeM = dataArray[i];
 //设置内容
 NSString *content = timeLimeM.text;
//计算高度,注意这里的宽度很关键,将会控制你文字设置的高度,options设置为:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading

 CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;

2.当计算下一个圆圈的y值时,算法如下:

- (CGFloat)iyToDataArray:(NSArray <GTimeLineViewModel *> *)dataArray toIndex:(NSInteger)index{
    CGFloat iy = 0;
    GTimeLineViewModel *viewModel;
    if (index > 0) {
        CGFloat contentH = 0;

//计算下一个圆圈,关键点就在这里,当i=1公式为上一个的圆圈+上一个时间轴的高度,当i //== 2时公式为:
//(i- 1)+(i - 2)内容的高度,当i == 3时公式为:
//(i- 1)+(i - 2)+(i-3)内容的高度等等。
        for (NSInteger i = index; i > 0; i--) {
             viewModel= [dataArray objectAtIndex:(i-1)];
            NSString *content = viewModel.text;
            CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[self FontSizeHeight:14]} context:nil].size;
            contentH = contentH+size.height;
        }
//        CGFloat textH = [self compareHeigth:contentH];
        iy = index * (KTIMELINEAUX_WH_CIRCLE+ 2*[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE]+KTIMELINEAUX_WGAP_LEFT)+contentH;
    }else{//为0时,y值 就是0
        viewModel = dataArray[index];
        NSString *content = viewModel.text;
        CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[self FontSizeHeight:14]} context:nil].size;
        iy = index * (KTIMELINEAUX_WH_CIRCLE+size.height+ 2*[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE]+KTIMELINEAUX_WGAP_LEFT);
    }
    return iy;
}

3.下面就时间轴制作的整套代码

#define  TIME_SCERRN_WIDTH [UIScreen mainScreen].bounds.size.width
#define  TIME_SCERRN_HEIGHT [UIScreen mainScreen].bounds.size.height
#import "GTimeLineView.h"
#import "GTimeLineViewModel.h"
#import "KTimeLineAux.h"
#import "UIImage+Custom.h"
@interface GTimeLineView(){

    UILabel *_circleL; //圆圈
    UILabel *_lineL;   //竖线
    UILabel *_twoCircleL; //圆圈2
    UILabel *_timeL;  //时间label
    UIImageView *_conentImageV; //存储label文字
    UILabel *_conentL;  //label文字
    KTimeLineAux *_ktimeLineAux;

}
@end
@implementation GTimeLineView

- (void)timeLineViewWithFrame:(CGRect)frame reloadDataWithArray:(NSArray<GTimeLineViewModel *> *)dataArray{

    if (dataArray && dataArray.count > 0) {
        [self loadUI:frame];
        for (NSInteger i = 0; i < dataArray.count; i++) {
            id sub = dataArray[i];
            if (![sub isKindOfClass:[GTimeLineViewModel class]]) {
                return;
            }
            GTimeLineViewModel *timeLimeM = dataArray[i];

            NSString *content = timeLimeM.text;
            if (!content.length) {
                return;
            }
            CGFloat imageW = [self getImageViewWidth];

            CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
            //线的高度
            CGFloat lineHeight = 2*[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE]+size.height+KTIMELINEAUX_WGAP_LEFT;

            CGFloat circleY = [self iyToDataArray:dataArray toIndex:i];

            [self setFrame:frame];
            //one circle
            _circleL = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,circleY,KTIMELINEAUX_WH_CIRCLE, KTIMELINEAUX_WH_CIRCLE)];
            [self setCornerRadiusWithLabel:_circleL];
            _circleL.layer.borderColor = [self sixtyTwoColor].CGColor;
            _circleL.layer.borderWidth = 2;

            //line

            _lineL = [[UILabel alloc] initWithFrame:CGRectMake(_circleL.center.x-KTIMELINEAUX_WIDTH_LINE/2,CGRectGetMaxY(_circleL.frame),KTIMELINEAUX_WIDTH_LINE,lineHeight)];
            [_lineL setBackgroundColor:[self sixtyTwoColor]];

            //time
            _timeL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_circleL.frame)+[_ktimeLineAux kTimeLineAuxScaleWidth:KTIMELINEAUX_WGAP_CIRCLE_TIME],CGRectGetMinY(_circleL.frame)-2,KTIMELINEAUX_WIDTH_TIME_LABEL,KTIMELINEAUX_HEIGHT_TIME_LABEL)];
            [_timeL setTextColor:[self sixtyTwoColor]];
            _timeL.text = timeLimeM.time;
            [_timeL setFont:[self FontSizeHeight:16]];
            _timeL.textAlignment = NSTextAlignmentLeft;

            //image

            _conentImageV = [[UIImageView alloc] initWithFrame:CGRectMake([_ktimeLineAux kTimeLineAuxScaleWidth:KTIMELINEAUX_WGAP_IMAGE_LEFT],CGRectGetMaxY(_circleL.frame)+[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE],imageW,size.height+10)];
            _conentImageV.image = [UIImage resizeableImageWithName:@"timeImage"];

            _conentL = [[UILabel alloc] initWithFrame:CGRectMake(KTIMELINEAUX_WGAP_LEFT, KTIMELINEAUX_WGAP_LEFT,CGRectGetWidth(_conentImageV.frame)-2*KTIMELINEAUX_WGAP_LEFT, size.height)];
            _conentL.numberOfLines = 0;
            _conentL.lineBreakMode = NSLineBreakByCharWrapping;
            _conentL.text = content;
            [_conentL setTextColor:[self fivtyOneColor]];
            [_conentImageV addSubview:_conentL];

            if (i == dataArray.count -1) {
                _twoCircleL = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,CGRectGetMaxY(_lineL.frame),KTIMELINEAUX_WH_CIRCLE, KTIMELINEAUX_WH_CIRCLE)];
                [self setCornerRadiusWithLabel:_twoCircleL];
                _twoCircleL.layer.borderColor = [self sixtyTwoColor].CGColor;
                _twoCircleL.layer.borderWidth = 2;
                [_twoCircleL setBackgroundColor:[UIColor clearColor]];

                //change Frame
                CGRect selfF = self.frame;
                selfF.size.height = CGRectGetMaxY(_twoCircleL.frame)+10;
                self.frame = selfF;

//                [self setContentSize:CGSizeMake(frame.size.width,3000)];
            }
//            [_scrollView addSubview:self];
            [self addSubview:_circleL];
            [self addSubview:_lineL];
            [self addSubview:_twoCircleL];
            [self addSubview:_timeL];
            [self addSubview:_conentImageV];
            [self addSubview:_twoCircleL];
            NSLog(@"\n_circleL= %@\n_lineL =%@\n_timeL=%@\n_conentImageV=%@\n_twoCircleL=%@\nself=%@\n",_circleL,_lineL,_timeL,_conentImageV,_twoCircleL,self);

        }
    }

}
#pragma mark --- loadUI
- (void)loadUI:(CGRect)frame{
    _ktimeLineAux = [KTimeLineAux shareTimeLineAux];

    CGFloat iy = 0;
    if (frame.size.height <TIME_SCERRN_HEIGHT) {
        iy = TIME_SCERRN_HEIGHT;
    }else{
        iy = frame.size.height;
    }
}
#pragma mark --- font
- (UIFont*)FontSizeHeight:(CGFloat)height{

    return [UIFont systemFontOfSize:height];
}
#pragma mark --- Radius
- (void)setCornerRadiusWithLabel:(UILabel *)label{
    label.layer.cornerRadius  = KTIMELINEAUX_WH_CIRCLE/2;
    label.layer.masksToBounds = YES;
}
#pragma mark --- color
- (UIColor *)fivtyOneColor{
    return [self colorWithR:51 G:51 B:51];
}
- (UIColor *)sixtyTwoColor{

    return [self colorWithR:62 G:147 B:75];
}
- (UIColor *)colorWithR:(CGFloat)r G:(CGFloat)g B:(CGFloat)b{

    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
}

//_circly iy
- (CGFloat)iyToDataArray:(NSArray <GTimeLineViewModel *> *)dataArray toIndex:(NSInteger)index{
    CGFloat iy = 0;
    GTimeLineViewModel *viewModel;
    if (index > 0) {
        CGFloat contentH = 0;
        for (NSInteger i = index; i > 0; i--) {
             viewModel= [dataArray objectAtIndex:(i-1)];
            NSString *content = viewModel.text;
            CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[self FontSizeHeight:14]} context:nil].size;
            contentH = contentH+size.height;
        }
//        CGFloat textH = [self compareHeigth:contentH];
        iy = index * (KTIMELINEAUX_WH_CIRCLE+ 2*[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE]+KTIMELINEAUX_WGAP_LEFT)+contentH;
    }else{
        viewModel = dataArray[index];
        NSString *content = viewModel.text;
        CGSize size = [content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[self FontSizeHeight:14]} context:nil].size;
        iy = index * (KTIMELINEAUX_WH_CIRCLE+size.height+ 2*[_ktimeLineAux kTimeLineAuxScaleHeight:KTIMELINEAUX_HGAP_CIRCLE_IMAGE]+KTIMELINEAUX_WGAP_LEFT);
    }

    return iy;
}
- (CGFloat)getImageViewWidth{

   return (TIME_SCERRN_WIDTH - [_ktimeLineAux kTimeLineAuxScaleWidth:KTIMELINEAUX_WGAP_IMAGE_LEFT] - 2*KTIMELINEAUX_WGAP_RIGHT);
}
- (CGFloat)compareHeigth:(CGFloat)height{

    CGFloat h = 30;
    if (h > height)
        return h;
    else
       return  height;
}
@end
+ (UIImage *)resizeableImageWithName:(NSString *)name{

    UIImage *image = [UIImage imageNamed:name];
    NSInteger leftCapWidth = image.size.width *0.7;
    NSInteger topCapHeight = image.size.height *0.5;
    CGFloat left = 5;
    CGFloat right = 10;
    CGFloat top = 10;
    CGFloat bottom = 10;
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);

    return [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
//    return [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface KTimeLineAux : NSObject

extern const NSInteger KTIMELINEAUX_WIDTH_TIME_LABEL;          //时间的宽度
extern const NSInteger KTIMELINEAUX_WIDTH_LINE;          //线的宽度
extern const NSInteger KTIMELINEAUX_WH_CIRCLE;           //圆圈的大小

extern const NSInteger KTIMELINEAUX_HEIGHT_TIME_LABEL;   //时间的高度

extern const NSInteger KTIMELINEAUX_WGAP_LEFT;          //视图左边间距
extern const NSInteger KTIMELINEAUX_WGAP_RIGHT;         //视图右边间距
extern const NSInteger KTIMELINEAUX_HGAP_CIRCLE_IMAGE;  //圆圈与存储文字的视图的上下间距

extern const NSInteger KTIMELINEAUX_WGAP_CIRCLE_TIME;    //圆圈与时间label的间距
extern const NSInteger KTIMELINEAUX_WGAP_LINE_IMAGE;     //线与存储文字视图的左右间距
extern const NSInteger KTIMELINEAUX_WGAP_IMAGE_LEFT;    //存储文字视图左边间距
extern const NSInteger KTIMELINEAUX_WGAP_IMAGE_RIGHT;   //存储文字视图右边间距

+ (KTimeLineAux *)shareTimeLineAux;

/**按比例获取高度*/
- (CGFloat)kTimeLineAuxScaleHeight:(CGFloat)height;
/**按比例获取宽度*/
- (CGFloat)kTimeLineAuxScaleWidth:(CGFloat)width;
@end
#define SCREEN_WIDTH ([[UIScreen mainScreen]bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen]bounds].size.height)
#define SCREEN_WIDTH_SCALE SCREEN_WIDTH/375
#define SCREEN_HEIGHT_SCALE SCREEN_HEIGHT/667
#define SCREEN_DEFAULT_HEIGHT 667
#import "KTimeLineAux.h"

@implementation KTimeLineAux
NSInteger const KTIMELINEAUX_WIDTH_TIME_LABEL = 100;
NSInteger const KTIMELINEAUX_HEIGHT_TIME_LABEL = 20;

NSInteger const KTIMELINEAUX_WIDTH_LINE = 2;
NSInteger const KTIMELINEAUX_WH_CIRCLE = 8;

NSInteger const KTIMELINEAUX_HGAP_CIRCLE_IMAGE = 17;
NSInteger const KTIMELINEAUX_WGAP_LEFT = 10;
NSInteger const KTIMELINEAUX_WGAP_RIGHT = 10;
NSInteger const KTIMELINEAUX_WGAP_CIRCLE_TIME = 19;
NSInteger const KTIMELINEAUX_WGAP_IMAGE_LEFT = 33;
NSInteger const KTIMELINEAUX_WGAP_IMAGE_RIGHT = 10;

+ (KTimeLineAux *)shareTimeLineAux{

    static KTimeLineAux *timeLine = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        timeLine = [[KTimeLineAux alloc] init];
    });
    return timeLine;
}
- (CGFloat)kTimeLineAuxScaleHeight:(CGFloat)height{
    if (height<=0) {
        return 0;
    }
    CGFloat scaleH = height*SCREEN_HEIGHT_SCALE;
    return scaleH;
}
- (CGFloat)kTimeLineAuxScaleWidth:(CGFloat)width{

    if (width <= 0) {
        return 0;
    }
    CGFloat scaleW = width*SCREEN_WIDTH_SCALE;
    return scaleW;
}
@end
时间: 2024-10-16 22:52:46

时间轴的制作的相关文章

使用Java8提供的Duration类制作字幕时间轴调整工具

网上下载的字幕有时和片源的时间轴不一致,我们可以自己写一个工具来调整,也就是整体向前移动几秒,或者向后移动几秒.Java8中提供的Duration类使得这种时间计算极其方便.下面就以最简单的srt字幕格式为例说明. srt的时间轴格式如下: 1 00:00:36,800 --> 00:00:41,300 天下武功,皆出少林. 第一行是计数器,不用管. 第二行是时间轴,是我们的重点关注对象.这一行显示小时,分,秒,毫秒.然后用一个箭头隔开,表示从哪个时间到哪个时间显示第三行的字幕. java.ti

报表工具怎么制作带有时间轴的记录表?

想要制作一个时间轴记录事件的报表,通过报表工具要怎么制作呢?该怎么制作时间轴呢?下面我们就以润乾报表为例制作两种类型的时间轴记录表,具体看下如何实现. 简洁版时间轴 1. 增加数据集,将需要记录的时间和事件取到. 时间和事件数据我从 EXCEL 文件中读取,数据如下图所示: 报表设计器下新建一张报表,然后在“报表”菜单——数据集,中增加文件数据集. 选择 EXCEL 文件,然后选择字段点击确定. 建好数据集后,在报表设计界面右下角就可以看到新建的数据集字段名称,选中对应的字段分别拖拽到 B2,C

利用css制作横向和纵向时间轴

Html代码: <div class="container"> <p>原文地址:http://www.cnblogs.com/xiaofeixiang/</p> <ul class="time-horizontal"> <li><b></b>成立</li> <li><b></b>合作</li> <li><b

HTTP协议下可拖动时间轴播放FLV的实现(伪流媒体)

HTTP协议下实现FLV的播放其实并不复杂,当初实现的原理是使用了flowPlayer插件实现的,效果还不错.但仍有两大问题影响着客户的访问情绪: 1.预加载时页面卡死,似乎没有边下边播. 2.偶尔边下边播,却无法拖动时间轴至未下载的部分.相信很多人也遇到该问题. 一度想采用专门的媒体服务器如Adobe的FMS去实现该功能,后多方查找资料,发现采用媒体服务器成本较高,且效率并不是很好,各大视频网站也未采用该方式.而实现HTTP协议下播放flv并可拖动时间轴并非没有可能,关键在于以下几点: Flv

时间轴

在前端网看到个时间轴的效果,故简单模范了个. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> body{margin: 50px;} .container{margin-left: 30%;l

jQuery时间轴插件:jQuery Timelinr

前言 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚动.支持键盘方向键.经过扩展后可以支持鼠标滚轮事件. HTML 我们在body中建立一个div#timeline作为展示区,#dates为时间轴,示例中我们用年份作为主轴,#issues作为内容展示区,即展示对应主轴点年份的内容,注意id对应上. <div id="timeline"> <ul id="dates&

Android 时间轴

效果图: 数据是随便填的,显得有点乱,但是不影响效果.实现方面主要是用ListView来实现,主要是根据ListView的item位置与上一条数据进行比较,来控制时间的显示隐藏效果.思路很简单,下面看代码实现: 首先是页面的整体布局,很简单,就一个ListView: res/layout/activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and

android 简易时间轴(实质是ListView)

ListView的应用 1.在很多时候是要用到时间轴的,有些处理的时间轴比较复杂,这里就给出一个比较简单的时间轴,其实就是ListView里面的Item的设计. 直接上代码: ListView,item的xml文件-->time_item.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.andro

Jquery实现的几款漂亮的时间轴

引言 最近项目中使用了很多前端的东西,对于我一个做后台开发的人员,这是一个很好的锻炼的机会.经过这段时间的学习,感觉前端的东西太多了,太强大了,做出来的东西太炫酷了.现在有很多开源的前端框架,做的都非常的漂亮,h5发展了这么多年了,改变了互联网行业啊!下面给大家介绍几款漂亮的时间轴,也许大家以后工作中会用到. 一.纵向折叠时间轴 1.js文件(jQuery.js或者jQuery.min.js) 2.CSS文件   3.HTML代码   4.运行效果: 二.纵向鼠标滑动时间轴 1.js文件(jqu