UICollectionViewController 实现利用手势滑动选择

代码例子:http://pan.baidu.com/s/1bn06eOf   http://download.csdn.net/detail/qqmcy/6887385


Cell.h

#import <UIKit/UIKit.h>

@interface Cell : UICollectionViewCell
@property (strong, nonatomic) UILabel* label;
@end

Cell.m

//
//  Cell.m
//  CollectionViewDemo1
//
//  Created by 杜甲 on 14-1-26.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "Cell.h"

@implementation Cell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.contentView.frame = CGRectMake(0, 0, 50, 15);
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 15)];
        self.label.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont boldSystemFontOfSize:15];
        self.label.textColor = [UIColor colorWithRed:181.0 / 255.0 green:181.0 / 255.0 blue:181.0 / 255.0 alpha:1];
        [self.contentView addSubview:self.label];


        
        
    }
    return self;
}

@end

LineLayout.h

#import <UIKit/UIKit.h>

@interface LineLayout : UICollectionViewFlowLayout

@end

LineLayout.m

#import "LineLayout.h"
#define ITEM_SIZE 20
@implementation LineLayout
#define ACTIVE_DISTANCE 100
#define ZOOM_FACTOR 0


-(id)init
{
    self = [super init];
    if (self) {
        self.itemSize = CGSizeMake(50, 15);
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.sectionInset = UIEdgeInsetsMake(50, 30.0, 0, 50.0);
        self.minimumLineSpacing = 10;
        self.minimumInteritemSpacing = 70;
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UICollectionViewController
@property (strong , nonatomic) NSArray* m_provinceArr;
@property (strong , nonatomic) NSIndexPath * m_lastAccessed;

@end

MyViewController.m

//
//  MyViewController.m
//  CollectionViewDemo1
//
//  Created by 杜甲 on 14-1-26.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "MyViewController.h"
#import "Cell.h"
@interface MyViewController ()

@end

@implementation MyViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.m_provinceArr = @[@"北京",@"天津",@"上海",@"重庆",@"河北",@"山西",@"河北",@"辽宁",@"吉林",@"黑龙江",@"河北",@"江苏",@"浙江",@"安徽",@"福建",@"江西",@"山东",@"河南",@"湖北",@"湖南",@"广东",@"海南",@"四川",@"贵州",@"云南",@"陕西",@"甘肃",@"青海",@"*",@"广西",@"内蒙",@"宁夏",@"*"];
    self.view.frame = CGRectMake(0, 0, 275, 640);
    self.collectionView.allowsMultipleSelection = YES;
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"MY_CELL"];
    
    
    UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [self.collectionView addGestureRecognizer:panGesture];
    
    
}


-(void)panGesture:(UIGestureRecognizer*)gestureRecognizer
{
    float pointerX = [gestureRecognizer locationInView:self.collectionView].x;
    NSLog(@"pointerX = %f",pointerX);
    float pointerY = [gestureRecognizer locationInView:self.collectionView].y;
    for(Cell* cell1 in self.collectionView.visibleCells) {
        float cellLeftTop = cell1.frame.origin.x;
        NSLog(@"cellLeftTop = %f",cellLeftTop);
        float cellRightTop = cellLeftTop + cell1.frame.size.width;
        float cellLeftBottom = cell1.frame.origin.y;
        float cellRightBottom = cellLeftBottom + cell1.frame.size.height;
        
        if (pointerX >= cellLeftTop && pointerX <= cellRightTop && pointerY >= cellLeftBottom && pointerY <= cellRightBottom) {
            NSIndexPath* touchOver = [self.collectionView indexPathForCell:cell1];
            if (self.m_lastAccessed != touchOver) {
                [cell1.label setTextColor:[UIColor redColor]];
                
                if (cell1.selected) {
                    [self deselectCellForCollectionView:self.collectionView atIndexPath:touchOver];
                }
                else
                {
                    
                    [self selectCellForCollectionView:self.collectionView atIndexPath:touchOver];
                    
                }
            }
            
            self.m_lastAccessed = touchOver;
            
        }
    }
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        self.m_lastAccessed = nil;
        self.collectionView.scrollEnabled = YES;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell* cell = (Cell*)[collectionView cellForItemAtIndexPath:indexPath];

        [cell.label setTextColor:[UIColor redColor]];
        
   
    
    NSLog(@"indexPath.item = %d",indexPath.item);
}


/*Cell已经选择时回调*/
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell* cell = (Cell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell.label setTextColor:[UIColor colorWithRed:181.0 / 255.0 green:181.0 / 255.0 blue:181.0 / 255.0 alpha:1]];
}
/*Cell未选择时回调*/
-(void)selectCellForCollectionView:(UICollectionView*)collection atIndexPath:(NSIndexPath*)indexPath
{
    [collection selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    
    [self collectionView:collection didSelectItemAtIndexPath:indexPath];
}

-(void)deselectCellForCollectionView:(UICollectionView*)collection atIndexPath:(NSIndexPath*)indexPath
{
    [collection deselectItemAtIndexPath:indexPath animated:YES];
    [self collectionView:collection didDeselectItemAtIndexPath:indexPath];
    
}



- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return [self.m_provinceArr count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
    cell.label.text = [NSString stringWithFormat:@"%@",[self.m_provinceArr objectAtIndex:indexPath.item]];
    return cell;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "MyViewController.h"

@interface ViewController : UIViewController
@property (strong , nonatomic) MyViewController* m_v;

@end

ViewController.m

//
//  ViewController.m
//  CollectionViewDemo1
//
//  Created by 杜甲 on 14-1-26.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "ViewController.h"
#import "LineLayout.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    LineLayout* lineLayout = [[LineLayout alloc] init];
    
    self.m_v = [[MyViewController alloc] initWithCollectionViewLayout:lineLayout];
    [self.view addSubview:self.m_v.view];
}

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

@end















UICollectionViewController 实现利用手势滑动选择

上一篇:Altium Designer同一个工程里不同原理图导入到不同的PCB


下一篇:Spring RMI源码解读