美文网首页
第一个项目之七--UIScrollView

第一个项目之七--UIScrollView

作者: _ChengChengCh | 来源:发表于2015-02-11 14:57 被阅读77次

叙说前言


前面已经把路线显示出来了,动画也做好了。
但是好像有哪里不太对啊?没错,就是我路线怎么能只有一条呢?
那我要多加几条,这怎么办?好像哪里加都不对啊。
不如试一下左右滑动来选择不同的路线吧?就这么愉快地决定了!
<br />

在此之前


前面RouteViewController是直接显示了路线的,如果直接对RouteViewController做文章,是没办法实现左右滑动来更换视图的。那我们要怎么做呢?
还是先对RouteViewController作个打包。
在前面开发的时候也已经尽量对这个类封装好了,基本上,现在对这个类进行操作改动的,就是一个字符串数组,而我们只需要在调用的时候给这个类一个字符串数组就完成了!

RouteViewController.h
#import <UIKit/UIKit.h>

@interface RouteViewController : UIViewController

@property (nonatomic, strong) NSArray *array_station;

-(void)initData;

@end

在头文件里面加个参数,在初始化并push该类的时候赋值,然后就搞定了。
把RouteViewController.m里面的相同参数注释掉,initRouteView里面调用initData的我们也去掉,再在initData里面的初始化语句注释掉,完成了!
测试一下打包效果,我们改动一下RootViewController:

RootViewController.m
- (void)gotoRouteView{
    RouteViewController *instance_routeView = [[RouteViewController alloc]init];
    [instance_routeView.view setFrame:self.view.bounds];
    instance_routeView.array_station = [NSArray arrayWithObjects:@"起点", @"第一站", @"第二站", @"第三站", @"第四站", @"终点", nil];
    [instance_routeView initData];
    [self.navigationController pushViewController:instance_routeView animated:YES];
}

运行结果就不放上来了,跟昨天的是一样的。
某种意义上,这个RouteViewController就是一个不太好看的插件了。
<br />

大工程


下面就是加入这个更换路线的功能了!
首先,我们要新建一个RouteController并继承UIScrollView,然后再拓展<UIScrollViewDelegate>,这个类就是用来控制线路的显示,所有的路线都会集成到这里面去。
然后再新建一个RouteCenterViewController用来显示线路,并且调用控制器。
用UIScrollView的时候就是要这样的效果

Paste_Image.png

n个页面并排加在一个UIScrollView上面,通过Scroll来切换不同的视图。
然后再将PagingEnabled设为YES,就可以实现一个个页面的切换了。

RouteController.h
#import <UIKit/UIKit.h>

@interface RouteController : UIScrollView<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *array_routeView;

- (id)initWithFrame:(CGRect)frame;

- (void)initRouteViews;

@end
RouteController.m
#import "RouteController.h"
#import "CommonHandler.h"
#import "RouteViewController.h"

@interface RouteController()

@property (nonatomic, assign) CGFloat float_offsetX;

@end

@implementation RouteController

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.delegate = self;
        self.backgroundColor = [CommonHandler getColorWithRed:225 andGreen:225 andBlue:225 andAlpha:1];
        self.pagingEnabled = YES;
        self.userInteractionEnabled = YES;
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        self.float_offsetX = 0;
    }
    return self;
}

- (void)initRouteViews{
    for(int i = 0; i < [self.array_routeView count]; i++){
        RouteViewController *instance_temp = self.array_routeView[i];
        instance_temp.view.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - HEIGHT_NAVIGATIONBAR_STATUSBAR);
        [instance_temp initData];
        [instance_temp.view setTag:(90000 + i)];
        if(i == 0){
            instance_temp.view.backgroundColor = [CommonHandler getColorWithRed:(i * 10 + 10) andGreen:(i * 20 + 20) andBlue:(i * 30 + 30) andAlpha:1];
        }
        [self addSubview:instance_temp.view];
    }
    
    self.contentSize = CGSizeMake(self.frame.size.width * [self.array_routeView count], self.frame.size.height - HEIGHT_NAVIGATIONBAR_STATUSBAR);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.float_offsetX = scrollView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self showRouteView];
}

- (void)showRouteView{
    CGFloat float_pageWidth = self.frame.size.width;
    int page = floor((self.contentOffset.x - float_pageWidth / self.array_routeView.count) / float_pageWidth) + 1;
    RouteViewController *instance_temp = self.array_routeView[page];
    instance_temp.view.backgroundColor = [CommonHandler getColorWithRed:(page * 10 + 10) andGreen:(page * 20 + 20) andBlue:(page * 30 + 30) andAlpha:1];
}

@end

然后,我们就要对RouteCenterViewController下手了,目的是将装载有众多路线图的RouteController显示出来。

RouteCenterViewController.m
#import "RouteCenterViewController.h"
#import "RouteController.h"
#import "RouteViewController.h"

@interface RouteCenterViewController ()

@property (nonatomic, strong) NSMutableArray *array_view;
@property (nonatomic, strong) NSArray *array_stations;

@end

@implementation RouteCenterViewController

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

- (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) initData{
    self.array_stations = [NSArray arrayWithObjects:
                           [NSArray arrayWithObjects:@"珠海北",@"北理工",@"北师大",@"普陀寺", nil],
                           [NSArray arrayWithObjects:@"云顶澜山",@"普陀寺", @"UIC", @"官塘", @"金鼎", nil],
                           [NSArray arrayWithObjects:@"北理工",@"宁堂",@"北理工南门",@"京师家园",@"北师大", @"UIC",@"城轨唐家湾站", nil],
                           nil];
    self.array_view = [[NSMutableArray alloc]init];
    [self initRouteCenterView];
}

- (void)initRouteCenterView{
    RouteController *instance_routeController = [[RouteController alloc]init];
    [instance_routeController setFrame:self.view.bounds];
    for(int i = 0; i < self.array_stations.count; i++){
        RouteViewController *instance_temp = [[RouteViewController alloc]init];
        instance_temp.array_station = self.array_stations[i];
        [self.array_view addObject:instance_temp];
    }
    instance_routeController.array_routeView = self.array_view;
    [instance_routeController initRouteViews];
    [self.view addSubview:instance_routeController];
}

@end

头文件里面是保持默认,什么都没有。
实现文件里面,也只是将RouteController添加进去就完了。
稍微看看效果!

RouteViewMulti.gif

不太好看,但是目的达到了!
本篇就暂时到这里了!

相关文章

网友评论

      本文标题:第一个项目之七--UIScrollView

      本文链接:https://www.haomeiwen.com/subject/pmwjxttx.html