Appdelegate.Swift
//在AppDelegate中创建导航控制器,并设置它为根视图
let root:mainViewController = mainViewController()
let nav = UINavigationController(rootViewController: root)
self.window!.rootViewController = nav
mainViewController.h
@interface mainViewController : UITabBarController
mainViewController.m
#import "mainViewController.h"
#import "myViewController.h"
#import "myCollectionViewCell.h"
#import "oneViewController.h"
#import "twoViewController.h"
#import "threeViewController.h"
#import "fourViewController.h"
@interface mainViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
NSArray *ziarray;
}
@property(nonatomic,strong) UIScrollView *scrollview;
@property(nonatomic,strong) UIPageControl *pagecon;
@property(nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UICollectionView *collections;
@property (nonatomic,strong) UICollectionViewFlowLayout *flow;
@end
@implementation mainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"网格";
//小轮播图
[self initscroll];
//定时器
[self initTimer];
//添加网格视图
[self.view addSubview:self.collections];
ziarray = [NSArray arrayWithObjects:@"我的账户",@"转账",@"跨行通",@"民生付款码",@"缴费中心",@"投资理财",@"外汇",@"信用卡",@"直销银行",@"货款",@"基金",@"黄金银行", nil];
//首页控制器
oneViewController *one = [[oneViewController alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:one];
UITabBarItem *itm1 = [[UITabBarItem alloc]initWithTitle:@"手机银行" image:[UIImage imageNamed:@"4.png"] tag:0];
//第二个页面
twoViewController *two = [[twoViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:two];
UITabBarItem *itm2 = [[UITabBarItem alloc]initWithTitle:@"生活圈" image:[UIImage imageNamed:@"3.png"] tag:1];
//第三个页面
threeViewController *three = [[threeViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:three];
UITabBarItem *itm3 = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"5.png"] tag:2];
fourViewController *four = [[fourViewController alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:four];
UITabBarItem *itm4 = [[UITabBarItem alloc]initWithTitle:@"我" image:[UIImage imageNamed:@"6.png"] tag:3];
nav1.tabBarItem = itm1;
nav2.tabBarItem = itm2;
nav3.tabBarItem = itm3;
nav4.tabBarItem = itm4;
//添加到视图
self.viewControllers = @[nav1,nav2,nav3,nav4];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//封装UIScrollView 方法
- (void)initscroll{
//初始化
self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 220)];
//self.scrollview.backgroundColor = [UIColor redColor];
//添加到视图
[self.view addSubview:self.scrollview];
//设置滚动视图的长度
[self.scrollview setContentSize:CGSizeMake(CGRectGetWidth(self.view.bounds)*4, 0)];
for (NSInteger i = 0; i < 4; i++) {
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)*i,0, CGRectGetWidth(self.view.bounds), 220)];
// 设置图片
[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",(long)i]]];
[self.scrollview addSubview:imageview];
}
//设置图片的分页
[self.scrollview setPagingEnabled:YES];
//将图片水平面的横线隐藏
[self.scrollview setShowsHorizontalScrollIndicator:NO];
self.scrollview.delegate = self;
//设置滚动视图下面的点
self.pagecon = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollview.frame)-20, self.view.frame.size.width, 20)];
//设置的点数
[self.pagecon setNumberOfPages:4];
[self.pagecon setPageIndicatorTintColor:[UIColor blackColor]];
[self.view addSubview:self.pagecon];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = YES;
}
//调用 UIscrollview 的协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.pagecon setCurrentPage:(NSInteger)(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame))];
}
//封装的定时器
- (void)initTimer{
if (!self.timer) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];
//定时器会跑在标记为common modes的模式下,以上两种通用,像广告轮播等就会用到该模式。
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
/*self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
*/
}
}
-(void)click{
NSInteger pagecount = self.pagecon.currentPage + 1;
if (pagecount >= 4) {
pagecount = 0;
}
[self.scrollview setContentOffset:CGPointMake(pagecount * CGRectGetWidth(self.scrollview.frame), 0) animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (self.timer) {
//invalidate 废弃
[self.timer invalidate];
[self setTimer:nil];
}
}
//结束后返回第一张图片
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self initTimer];
}
#pragma mark - 网格视图
- (UICollectionView *)collections{
if (!_collections) {
_collections = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 300) collectionViewLayout:self.flow];
[_collections setDelegate:self];
[_collections setDataSource:self];
[_collections setBackgroundColor:[UIColor whiteColor]];
[_collections registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
}
return _collections;
}
- (UICollectionViewFlowLayout *)flow{
if (!_flow) {
_flow = [[UICollectionViewFlowLayout alloc]init];
[_flow setItemSize:CGSizeMake(80, 80)];
[_flow setMinimumLineSpacing:15];
[_flow setMinimumInteritemSpacing:10];
}
return _flow;
}
#pragma mark - 网格视图的 delegate datasour
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 12;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
myCollectionViewCell *mycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
//[mycell setBackgroundColor:[UIColor redColor]];
mycell.theimageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",indexPath.item]];
mycell.thelable.text = [NSString stringWithFormat:@"%@",ziarray[indexPath.row]];
return mycell;
}
@end
myCollectionViewCell.h
#import <UIKit/UIKit.h>
@interface myCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *theimageview;
@property(nonatomic,strong) UILabel *thelable;
@end
myCollectionViewCell.m
#import "myCollectionViewCell.h"
@implementation myCollectionViewCell
//懒加载
-(instancetype)initWithFrame:(CGRect)frame{
if ( self = [super initWithFrame:frame]) {
[self addSubview:self.theimageview];
[self addSubview:self.thelable];
}
return self;
}
//设置视图的图片
-(UIImageView *)theimageview{
if (!_theimageview) {
_theimageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
}
return _theimageview;
}
-(UILabel *)thelable{
if (!_thelable) {
_thelable = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 70, 22)];
_thelable.font = [UIFont systemFontOfSize:18];
_thelable.textColor = [UIColor blackColor];
_thelable.textAlignment = NSTextAlignmentCenter;
}
return _thelable;
}
@end
minsheng-Bridging-Header.h
# import "mainViewController.h"
网友评论