UICollectionView點擊 UICollectionView_基礎 九宮格

#import "ViewController.h"

@interface  ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//UICollectionViewDelegate是UICollectionViewDelegateFlowLayout的父類
{
    UICollectionView *_collectionView;
    NSString *CellIdentifier;//cell的重用標示符;
    NSInteger num;
    NSInteger CNum;
    UILabel *label;
    UILabel *labelLevel;
    UIButton *_button;
    NSTimer *_timer;
    int tim;
    NSInteger i;
    int nu;
    float a;
    
    float red;
    float green;
    float blue;
    
    float Cred;
    float Cgreen;
    float Cblue;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定義cell的標示符
    CellIdentifier = @"reuseCell";
    //頁面佈局
    [self layoutUI];
       num =arc4random()%66;
    CNum = arc4random()%3;
    i = 100;
    red  = arc4random()%255;
    green = arc4random()%255;
    blue = arc4random()%255;
    [self reloadMyData];
}
-(void)layoutUI{
    //建立採集視圖佈局方案
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 67, 375, 600) collectionViewLayout:flowLaout];
    _collectionView.backgroundColor = [UIColor orangeColor];
    _collectionView.delegate =self;
    _collectionView.dataSource = self;
    //註冊採集視圖所使用的cell類,並設置標示符
    //cell的初始化,系統完成
    //若是你想自定義cell,UICollectionViewCell ->cell類
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    [self.view addSubview:_collectionView];
    
    tim = 30;
    label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 90, 30)];
    label.backgroundColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.5];
    label.text =[NSString stringWithFormat:@"倒計時%ds",tim];
    [self.view addSubview:label];
    
    labelLevel = [[UILabel alloc]initWithFrame:CGRectMake(150, 20, 90, 30)];
    nu = 1;
    labelLevel.text  = [NSString stringWithFormat:@"第%d關",nu];
    [self.view addSubview:labelLevel];
    
    _button = [[UIButton alloc]initWithFrame:CGRectMake(300, 20, 50, 30)];
    _button.backgroundColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.5];

    [_button setTitle:@"開始" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
}

-(void)click{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeok) userInfo:nil repeats:YES] ;
    [_button setHidden:YES];
}
-(void)timeok{
    
    tim--;
    label.text =[NSString stringWithFormat:@"倒計時%ds",tim];
    
    if (tim == 0) {
        _button.hidden = NO;
        label.text = @"結束";
        [_timer invalidate];
        _timer = nil;
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"遊戲結束" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        tim = 30;
        
    }
}
#pragma mark 設置單個section裏元素個數
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 60;
}
#pragma mark 設置cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //從隊列中取出一個cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    
//    NSLog(@"%li",(long)num);
    if (indexPath.row == num) {
       
        cell.backgroundColor = [UIColor colorWithRed:Cred/255.0 green:Cgreen/255.0 blue:Cblue/255.0 alpha:1];
        NSLog(@"%ld",indexPath.row);
    }
    else{
        cell.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
    }
    
    
    return cell;
}



//顏色  正常:red = 0-255 green blue   特殊:0-2      redC = red + 20 greenC = green blueC = blue


#pragma mark cell的點擊事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

    if (indexPath.row == num) {
        NSLog(@"第%ld格",indexPath.row);
        
        [self reloadMyData];
    }
   
}

-(void)reloadMyData
{
    
    num =arc4random()%66;
    //        colorNum = arc4random()%1000 +1;
    i = i - 2 > 0? i - 2 : 0;
    CNum = arc4random()%3;
    red  = arc4random()%255;
    green = arc4random()%255;
    blue = arc4random()%255;
    switch (CNum) {
        case 0:
            Cred = red+i;
            Cgreen =green +i;
            Cblue = blue;
            break;
        case 1:
            Cred = red;
            Cgreen =green + i;
            Cblue = blue + i;
            break;
        case 2:
            Cred = red+i;
            Cgreen =green;
            Cblue = blue + i;
            break;
        default:
            break;
    }
    
    NSLog(@"%f %f %f %f %f %f", red,green,blue,Cred,Cgreen,Cblue);
    
    labelLevel.text = [NSString stringWithFormat:@"第%d關",nu];
    ++nu;
    [_collectionView reloadData];
}



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *_imageArr = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            CGRect rect = CGRectMake(18.75 + j*118.75, 20+i*118.75, 100, 100);
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
            imageView.backgroundColor = [UIColor purpleColor];
            [self.view addSubview:imageView];
            [_imageArr addObject:imageView];
        }
    }
    NSLog(@"%@",_imageArr);
    UIImageView *iv = (UIImageView *)_imageArr[4];
    iv.backgroundColor = [UIColor redColor];
}

dom



#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//UICollectionViewDelegate是UICollectionViewDelegateFlowLayout的父類
{
    UICollectionView *_collectionView;
    NSString *CellIdentifier;//cell的重用標示符;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定義cell的標示符
    CellIdentifier = @"reuseCell";
    //頁面佈局
    [self layoutUI];
}
-(void)layoutUI{
    //建立採集視圖佈局方案
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    //設置滾動方向(水平或垂直)
//    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    flowLaout.scrollDirection
    //初始化collectionView
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, 375, 647) collectionViewLayout:flowLaout];
    _collectionView.backgroundColor = [UIColor orangeColor];
    _collectionView.delegate =self;
    _collectionView.dataSource = self;
    //註冊採集視圖所使用的cell類,並設置標示符
    //cell的初始化,系統完成
    //若是你想自定義cell,UICollectionViewCell ->cell類
    _collectionView.pagingEnabled = YES;//每次滾動只滑動一格ide

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    
    [self.view addSubview:_collectionView];
}
#pragma mark 設置單個section裏元素個數
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 66;
}
#pragma mark 設置cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //從隊列中取出一個cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.7 green:indexPath.row/(66 *1.0)-0.1 blue:0.5 alpha:1];
//    cell.backgroundColor = [UIColor colorWithWhite:indexPath.row/(66 *1.0) alpha:1];
    //實現點擊cell後改變選中狀態(設置選中時的狀態)
    UIView *selectedBGview = [[UIView alloc]initWithFrame:cell.bounds];
    selectedBGview.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = selectedBGview;
    
    return cell;
}
#pragma mark cell的點擊事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"第%ld格",indexPath.row);
    //經過indexpath獲取cell
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
}
#pragma mark 定義每一個cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(40, 40);
}
#pragma mark 定義每一個 section 的內邊距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark 設定cell行間距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}
#pragma mark 設定cell的最小間距(非行間距)
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}
//交互
//事件的處理順序
//1.手指按下某個cell
//2.shouldHighlightItemAtIndexPath(若是返回yes,則向下執行,不然終止)
//3.didHighlightItemAtIndexPath(高亮)
//4.手指鬆開
//5.didUnhighlightItemAtIndexPath(取消高亮)
//6.sholdSelectItemAtIndexPath(若是返回yes,則向下執行,不然終止)
//7.didSelectItemAtIndexPath(執行選擇事件)
-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"亮了");
    
}
-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"取消高亮");
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//頁眉
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 0);//垂直滾動第二個參數,水平第一個
}
//頁腳
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(0, 0);
}

佈局

相關文章
相關標籤/搜索