第一个项目之五--RouteViewController

RouteViewController.m


RouteViewController是继承UIViewController。所以RouteviewController具有了UIViewController的特性,也能扩展属于自己的特性。
现在对上次那个空白的RouteViewController做文章了!

#import "RouteViewController.h"
#import "CommonHandler.h"

@interface RouteViewController ()

@end

@implementation RouteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initRouteView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initRouteView{
    [self.view setFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [CommonHandler getColorWithRed:240 andGreen:240 andBlue:240 andAlpha:1];
    
    [self showMessage];
}

- (void)showMessage{
    UILabel *lb_center_message = [[UILabel alloc]init];
    lb_center_message.backgroundColor = [CommonHandler getColorWithRGB:0xFFE4C4];
    lb_center_message.textColor = [UIColor blackColor];
    lb_center_message.font = [UIFont fontWithName:@"Arial" size:18.0];
    lb_center_message.text = @"Welcome";
    CGSize size_message = [lb_center_message.text sizeWithAttributes:@{NSFontAttributeName:lb_center_message.font}];
    [lb_center_message setFrame:CGRectMake((self.view.frame.size.width - size_message.width) / 2, (self.view.frame.size.height - size_message.height) / 2, size_message.width, size_message.height)];
    [self.view addSubview:lb_center_message];
}

@end

效果就是这样了:



上面有个居中显示,每次这样手输也太累了,我决定也把它打包到CommonHandler里面去!
我们在CommonHandler里面加上这个方法:

+(CGRect)getCenterRectWithSuperView:(UIView *)superView andContent:(NSString *)content andFont:(UIFont *)font{
    CGSize size_content = [content sizeWithAttributes:@{NSFontAttributeName:font}];
    CGRect rect_content = CGRectMake((superView.frame.size.width - size_content.width) / 2, (superView.frame.size.height - size_content.height) / 2, size_content.width, size_content.height);
    return rect_content;
}

地铁线


下面开始绘制RouteViewController了。
先将数据打包一下,然后将起点和终点先弄出来:
RouteViewController.m

- (void)initData{
    self.array_station = [NSArray arrayWithObjects:@"起点", @"第一站", @"第二站", @"第三站", @"终点", nil];
    self.number_station = [NSNumber numberWithLong:[self.array_station count] - 2];
    
    [self drawRoute];
}

- (void)drawRoute {
    CGSize size_label = CGSizeMake(120, 40);
    
    UILabel *lb_start = [[UILabel alloc]init];
    
    lb_start.text = self.array_station[0];
    lb_start.textColor = [UIColor whiteColor];
    lb_start.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_start.textAlignment = NSTextAlignmentCenter;
    
    lb_start.layer.backgroundColor = [CommonHandler getColorWithRGB:0x18aaf2].CGColor;
    lb_start.layer.cornerRadius = 20.0;
    [lb_start.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:(HEIGHT_NAVIGATIONBAR_STATUSBAR + GAP_DEFAULT) andSize:size_label]];
    [self.view addSubview:lb_start];
    
    UILabel *lb_end = [[UILabel alloc]init];
    
    lb_end.text = [self.array_station lastObject];
    lb_end.textColor = [UIColor whiteColor];
    lb_end.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_end.textAlignment = NSTextAlignmentCenter;
    
    lb_end.layer.backgroundColor = [UIColor redColor].CGColor;
    lb_end.layer.cornerRadius = 20.0;
    [lb_end.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:self.view.frame.size.height - GAP_DEFAULT - size_label.height andSize:size_label]];
    
    [self.view addSubview:lb_end];
}

效果就出来了

Paste_Image.png

起点终点摆放还是很简单的。然后就是将中间的站点添加上去了,但是中间站点和两个固定的不同,数量是不定的,所以要做一个匹配。
RouteViewController.m

- (void)drawRoute{
    CGSize size_label = CGSizeMake(120, 40);
    
    UILabel *lb_start = [[UILabel alloc]init];
    
    lb_start.text = self.array_station[0];
    lb_start.textColor = [UIColor whiteColor];
    lb_start.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_start.textAlignment = NSTextAlignmentCenter;
    
    lb_start.layer.backgroundColor = [CommonHandler getColorWithRGB:0x18aaf2].CGColor;
    lb_start.layer.cornerRadius = 20.0;
    [lb_start.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:(HEIGHT_NAVIGATIONBAR_STATUSBAR + GAP_DEFAULT) andSize:size_label]];
    [self.view addSubview:lb_start];
    
    UILabel *lb_end = [[UILabel alloc]init];
    
    lb_end.text = [self.array_station lastObject];
    lb_end.textColor = [UIColor whiteColor];
    lb_end.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_end.textAlignment = NSTextAlignmentCenter;
    
    lb_end.layer.backgroundColor = [UIColor redColor].CGColor;
    lb_end.layer.cornerRadius = 20.0;
    [lb_end.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:self.view.frame.size.height - GAP_DEFAULT - size_label.height andSize:size_label]];
    
    [self.view addSubview:lb_end];
    
    CGFloat height_middle = (lb_end.frame.origin.y + (lb_end.frame.size.height / 2)) - (lb_start.frame.origin.y + (lb_start.frame.size.height / 2));
    CGFloat gap_station = height_middle / ([self.number_station intValue] + 1);
    
    for(int i = 0; i < [self.number_station intValue]; i++){
        UILabel *lb_temp = [[UILabel alloc]init];
        lb_temp.text = self.array_station[i + 1];
        lb_temp.textAlignment = NSTextAlignmentCenter;
        lb_temp.textColor = [UIColor whiteColor];
        lb_temp.font = [UIFont fontWithName:@"Arial" size:15.0];
        
        lb_temp.layer.backgroundColor = [UIColor orangeColor].CGColor;
        lb_temp.layer.cornerRadius = 15.0;
        [lb_temp.layer setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:((lb_start.frame.origin.y + (lb_start.frame.size.height / 2)) + (gap_station * (i + 1))) - (size_label.height / 2) andSize:size_label]];
        
        [self.view addSubview:lb_temp];
    }
}

有3个中途站的时候的效果。

Paste_Image.png

而有6个中途站的时候:

self.array_station = [NSArray arrayWithObjects:@"起点", @"第一站", @"第二站", @"第三站", @"第四站", @"第五站", @"第六站", @"终点", nil];
Paste_Image.png

上面写了,这个是地铁路线图嘛,只有站点,没有铁路,好像还是差点什么东西。我们来给中间加上铁路线好了!

- (void)drawRoute{
    CGSize size_route = CGSizeMake(5, 5);
    CGFloat height_route = gap_station - size_label.height;
    CGFloat gap_route = height_route / size_route.height;
    
    for(int i = 0; i < [self.number_station intValue] + 1; i++){
        for(int j = 0; j < (int)gap_route; j++){
            UILabel *lb_temp = [[UILabel alloc]init];
            if(j % 2 == 0){
                lb_temp.backgroundColor = [UIColor blackColor];
            }else{
                lb_temp.backgroundColor = [UIColor yellowColor];
            }
            [lb_temp setFrame:[CommonHandler getVerticaRectlWithSuperView:self.view andYOrigin:(lb_start.frame.origin.y + lb_start.frame.size.height + ((height_route + size_label.height) * i) + (size_route.height * j)) andSize:size_route]];
            [self.view addSubview:lb_temp];
        }
    }
}

补上了这段之后,铁路线就出来啦!

Paste_Image.png

<br />

最后


恩,的确是不太美观。追求完美的童鞋可以再去做做美化!
这篇就到这里了。下篇就要用下手势事件了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容