混編網(wǎng)格

Appdelegate.Swift

//在AppDelegate中創(chuàng)建導(dǎo)航控制器,并設(shè)置它為根視圖
        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 = @"網(wǎng)格";
    //小輪播圖
    [self initscroll];
    //定時(shí)器
    [self initTimer];
    
    
    //添加網(wǎng)格視圖
    [self.view addSubview:self.collections];
    
    ziarray = [NSArray arrayWithObjects:@"我的賬戶",@"轉(zhuǎn)賬",@"跨行通",@"民生付款碼",@"繳費(fèi)中心",@"投資理財(cái)",@"外匯",@"信用卡",@"直銷銀行",@"貨款",@"基金",@"黃金銀行", nil];
    
    //首頁(yè)控制器
    oneViewController *one = [[oneViewController alloc]init];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:one];
    UITabBarItem *itm1 = [[UITabBarItem alloc]initWithTitle:@"手機(jī)銀行" image:[UIImage imageNamed:@"4.png"] tag:0];
    
    //第二個(gè)頁(yè)面
    twoViewController *two = [[twoViewController alloc]init];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:two];
    
    UITabBarItem *itm2 = [[UITabBarItem alloc]initWithTitle:@"生活圈" image:[UIImage imageNamed:@"3.png"] tag:1];
    
    //第三個(gè)頁(yè)面
    threeViewController *three = [[threeViewController alloc]init];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:three];
    UITabBarItem *itm3 = [[UITabBarItem alloc]initWithTitle:@"發(fā)現(xiàn)" 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];
    
    //設(shè)置滾動(dòng)視圖的長(zhǎng)度
    [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)];
        // 設(shè)置圖片
        [imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",(long)i]]];
        [self.scrollview addSubview:imageview];
    }
    //設(shè)置圖片的分頁(yè)
    [self.scrollview setPagingEnabled:YES];
    //將圖片水平面的橫線隱藏
    [self.scrollview setShowsHorizontalScrollIndicator:NO];
    self.scrollview.delegate = self;
    
    //設(shè)置滾動(dòng)視圖下面的點(diǎn)
    self.pagecon = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollview.frame)-20, self.view.frame.size.width, 20)];
    //設(shè)置的點(diǎn)數(shù)
    [self.pagecon setNumberOfPages:4];
    [self.pagecon setPageIndicatorTintColor:[UIColor blackColor]];
    [self.view addSubview:self.pagecon];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
}

//調(diào)用 UIscrollview 的協(xié)議方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.pagecon setCurrentPage:(NSInteger)(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame))];
}
//封裝的定時(shí)器
- (void)initTimer{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];
        //定時(shí)器會(huì)跑在標(biāo)記為common modes的模式下,以上兩種通用,像廣告輪播等就會(huì)用到該模式。
        [[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];
    }
}
//結(jié)束后返回第一張圖片
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self initTimer];
}
#pragma mark - 網(wǎng)格視圖
- (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 - 網(wǎng)格視圖的 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;
}
//設(shè)置視圖的圖片
-(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"
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • OC中使用Swift 1、在項(xiàng)目中右鍵添加一個(gè) Swift 文件,如TestSwift.swift。 2、添加后 ...
    刺客辣條閱讀 2,277評(píng)論 3 11
  • 有時(shí)候當(dāng)我們用到Swift寫的第三方庫(kù)時(shí),就不得不用到Swfit和OC混編。 舉個(gè)??,如果你想在你的工程里用到Ch...
    helloxiaogui閱讀 2,364評(píng)論 0 6
  • 談到軍訓(xùn),由陌路到相識(shí),再由結(jié)識(shí)到之后的依舍難分,經(jīng)歷了一個(gè)雖短暫卻不缺乏情趣的階段。此刻依舊是品茶之味,似挽余香...
    fighting_c241閱讀 199評(píng)論 0 0
  • 作為軟件研發(fā)人員,我們是軟件產(chǎn)品的直接締造者,軟件產(chǎn)品都要經(jīng)我們之手來實(shí)現(xiàn)。所以,在軟件研發(fā)行業(yè),研發(fā)人員的生產(chǎn)效...
    藍(lán)灰_q閱讀 10,433評(píng)論 2 12
  • 我以為我的心死了,腐爛了,我再也不會(huì)愛上別人了。我再也不會(huì)有心動(dòng)的感覺,留下的只是算計(jì)。可是剎那間我感覺到全身的血...
    伊麗莎白瓜閱讀 115評(píng)論 0 0