UICollectionViewController 实现利用手势滑动选择
来源:互联网 发布:国家大数据项目 编辑:程序博客网 时间:2024/05/29 19:43
代码例子: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
0 0
- UICollectionViewController 实现利用手势滑动选择
- android 利用左右滑动手势实现avtivity的跳转
- view实现手势滑动
- ViewFlipper实现手势滑动
- 用ViewFlipper实现手势滑动
- android_手势滑动的实现
- Android 实现view手势滑动
- 手势滑动选择日期插件详解
- 利用UIPanGestureRecognizer 判断手势滑动方向
- 利用UIPanGestureRecognizer 判断手势滑动方向
- NumberPicker实现滑动选择
- UICollectionViewController
- UICollectionViewController
- tabhost使用viewflipper实现手势滑动分页
- html5手势滑动实现(单点)
- Android使用GestureDetector实现手势滑动效果
- Android 实现手势滑动界面(ViewPager)
- ViewPager+Handler实现轮播加手势滑动
- Android Looper和Handler
- Oracle 导出和导入
- SQL主外键约束
- Pat(Basic Level)Practice--1014(福尔摩斯的约会)
- CCCallFuncN误用导致引用计数循环引用
- UICollectionViewController 实现利用手势滑动选择
- 45度地图遮挡问题解决方案(cocos2d-x)
- 关于phpstorm 对yii的代码提示
- 45度地图游戏的地图拖动和主角跟随
- JS 将数字类型转化为没3个一个逗号的金钱格式
- LaTeX新人教程,30分钟从完全陌生到基本入门
- 客户端的web技术
- 接口的显示实现,隐式实现区别
- 黑马程序员----简单的多线程聊天框的编写