亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

使用iOS控件UICollectionView生成可拖動的桌面的實例

2020-07-26 03:02:21
字體:
來源:轉載
供稿:網友

一個App受歡迎的程度,一方面來源于它本身為用戶提供便捷的功能,另一方面則來源于它的UI。UI是用戶體驗重要的組成部分,構成UI的的元素恰恰離不開那些看似獨立的控件。在開發的過程中,大家對UITableView應該很熟悉吧!確實UITableView在處理數據顯示方面有著很強大的功能,例如網紅們使用的微博,微信社交軟件的聊天界面等等,這種流式布局使用UITableView簡直最合適不過了;但畢竟UITableView不是萬能的,當需要顯示橫縱向的數據時它就顯得捉襟見肘了,雖然這也難不倒我們程序猿但是何必要大費周章的去定義復雜的cell呢!UICollectionView就是專門用來應付這種布局的,使用UICollectionView可以給我們帶來以下幾點優勢:1.可以高度的定制內容展示的樣式 2.高效的管理大量的數據。

首先給大家看一下這個Demo的效果圖:

iOS設備不知道現在有沒有可以屏幕錄制的app,這樣我就可以把操作的動作用gif圖片po上來了,大家如果有推薦可以告訴我哈!關于拖動,長按圖片后就可以將圖片拖到你想要的位置上,另外的圖片則會依次排序,很順暢的體驗。

在使用UICollectionView的時后,我們的類需要實現這些協議:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,

UICollectionViewDelegate,UIGestureRecognizerDelegate。

UICollectionViewDataSource:和我們在UITableView中所需要實現的UITableViewDataSource是一個道理,它里面包括以下這些API:

@protocol UICollectionViewDataSource <NSObject> @required  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;  // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;  @optional  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;  // The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;  - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);  @end 

UICollectionViewDelegateFlowLayout:UICollectionViewFlowLayout是一個專門用來管理collectionView布局的類,可以通過實現以下函數來調整我們界面的樣式:

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate> @optional  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section; - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section; - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section; - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;  @end 

UICollectionViewDelegate:和UITableViewDelegate一樣,這里就把它協議里面的的函數po出來了,通過重寫里面的函數,我們可以實現cell的點擊與拖動。

UIGestureRecognizerDelegate:由于我們還有一個拖動的功能,所以需要實現用戶手勢的協議。

好了,基礎的概念講了,現在我們就開始動手實現他吧!老樣子直接看源碼:

- (void)viewDidLoad {   [super viewDidLoad];   // Do any additional setup after loading the view, typically from a nib.      //設置背景色   [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"back.jpg"]]];   //設置數據源   self.dataSource = [[NSMutableArray alloc] initWithObjects:             @"1.png",@"2.png",@"3.png", @"4.png", @"5.png", @"6.png", @"7.png", @"8.png", @"9.png", @"10.png", @"11.jpg", @"12.JPG",             @"13.JPG", @"14.jpg", @"15.JPG", @"16.png", @"17.png", @"18.png", @"19.png", @"20.png", nil nil];   //初始化布局   self.flow = [[UICollectionViewFlowLayout alloc] init];   self.collect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flow];   [self.collect setBackgroundColor:[UIColor clearColor]];      //注冊cell 這一步必須要實現   [self.collect registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:@"CustomCell"];      self.collect.delegate = self;   self.collect.dataSource = self;      [self.collect setFrame:self.view.bounds];      //添加長按手勢   self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];   [self.longPressGestureRecognizer addTarget:self action:@selector(handleLongPressRecognizer:)];   [self.collect addGestureRecognizer:self.longPressGestureRecognizer];      [self.view addSubview:self.collect]; } 

在viewDidLoad函數中,初始化了一個UICollectionViewFlowLayout布局,并且需要配合UICollectionView來使用,兩者加一起來使用才能“完美”,想比UITableView 中Cell使用的不同,在UICollectionView中必須先對Cell進行注冊(RegisterClass),不然會在運行過程中報錯。如何使排列的圖片可以拖動呢,在這里我為UICollectionView添加了一個長按手勢UILongPressGestureRecognizer。當我們長按時會觸發handleLongPressRecognizer,代碼如下:

- (void)handleLongPressRecognizer:(UILongPressGestureRecognizer *)gesture{   switch (gesture.state) {     case UIGestureRecognizerStateBegan:{       NSIndexPath *path = [self.collect indexPathForItemAtPoint:[gesture locationInView:gesture.view]];       if(path == nil){         break;       }              [self.collect beginInteractiveMovementForItemAtIndexPath:path];     }       break;     case UIGestureRecognizerStateChanged:       [self.collect updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];       break;     case UIGestureRecognizerStateEnded:       [self.collect endInteractiveMovement];       break;     default:       [self.collect cancelInteractiveMovement];       break;   } } 

好看的界面才能抓住用戶的心,這里我自定義了Cell繼承自UICollectionViewCell,cell中展示的圖片會根據自身圖片的大小進行按比例縮放,這樣我們的桌面看上去就會有橫版圖片與豎版圖片,如果不自定義的話就都是方方正正的九宮格,還是花點心思自定義一下顯示效果吧!CustomCollectionCell的的代碼如下:

#import "CustomCollectionCell.h"  @implementation CustomCollectionCell @synthesize imageV = _imageV; @synthesize labelV = _labelV; @synthesize boundView = _boundView;  - (id)initWithFrame:(CGRect) frame{   self = [super initWithFrame:frame];   //init attributes   if(self){     [self setBackgroundColor:[UIColor clearColor]];     self.imageV = [[UIImageView alloc] initWithFrame:CGRectZero];     self.labelV = [[UILabel alloc] initWithFrame:CGRectZero];     self.boundView = [[UIView alloc] initWithFrame:CGRectZero];     [self.boundView setBackgroundColor:[UIColor whiteColor]];     [self.boundView addSubview:self.imageV];     [self addSubview:self.boundView];     [self addSubview:self.labelV];   }      return self; }  - (void)setImageWithText:(UIImage *)image text:(NSString *)text{   if(!image){     return;   }      CGFloat imgWidth = image.size.width;   CGFloat imgHeight = image.size.height;   CGFloat iconWidth = 0.0;   CGFloat iconHeight = 0.0;      if(imgWidth > imgHeight){     iconHeight = roundf(((self.frame.size.width - 16)*imgHeight)/imgWidth);     iconWidth = self.frame.size.width - 16;     [self.boundView setFrame:CGRectMake(0, self.frame.size.height - iconHeight - 16, iconWidth + 16, iconHeight + 16)];     [self.imageV setFrame:CGRectMake(8, 8, iconWidth, iconHeight)];   }else{     iconWidth = roundf(((self.frame.size.width - 16) *imgWidth)/imgHeight);     iconHeight = self.frame.size.height - 16;     [self.boundView setFrame:CGRectMake(roundf((self.frame.size.width - iconWidth)/2), 0, iconWidth + 16, iconHeight + 16)];     [self.imageV setFrame:CGRectMake(8, 8, iconWidth, iconHeight)];   }      [self.imageV setImage:image]; }  @end 

以上這些就是構成該Demo的重要組成部分了,UICollectionView還有很多的要素在這里面沒有講到,往后我還會再研究這個控件更加高級的的使用,本篇就好比是餐前的開胃小菜吧,希望大家喜歡。附上這個例子的源碼:

@implementation ViewController @synthesize dataSource = _dataSource; @synthesize longPressGestureRecognizer = _longPressGestureRecognizer; @synthesize flow = _flow; @synthesize collect = _collect;  - (void)viewDidLoad {   [super viewDidLoad];   // Do any additional setup after loading the view, typically from a nib.      //設置背景色   [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"back.jpg"]]];   //設置數據源   self.dataSource = [[NSMutableArray alloc] initWithObjects:             @"1.png",@"2.png",@"3.png", @"4.png", @"5.png", @"6.png", @"7.png", @"8.png", @"9.png", @"10.png", @"11.jpg", @"12.JPG",             @"13.JPG", @"14.jpg", @"15.JPG", @"16.png", @"17.png", @"18.png", @"19.png", @"20.png", nil nil];   //初始化布局   self.flow = [[UICollectionViewFlowLayout alloc] init];   self.collect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flow];   [self.collect setBackgroundColor:[UIColor clearColor]];      //注冊cell 這一步必須要實現   [self.collect registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:@"CustomCell"];      self.collect.delegate = self;   self.collect.dataSource = self;      [self.collect setFrame:self.view.bounds];      //添加長按手勢   self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];   [self.longPressGestureRecognizer addTarget:self action:@selector(handleLongPressRecognizer:)];   [self.collect addGestureRecognizer:self.longPressGestureRecognizer];      [self.view addSubview:self.collect]; }  - (void)didReceiveMemoryWarning {   [super didReceiveMemoryWarning];   // Dispose of any resources that can be recreated. }  - (void)handleLongPressRecognizer:(UILongPressGestureRecognizer *)gesture{   switch (gesture.state) {     case UIGestureRecognizerStateBegan:{       NSIndexPath *path = [self.collect indexPathForItemAtPoint:[gesture locationInView:gesture.view]];       if(path == nil){         break;       }              [self.collect beginInteractiveMovementForItemAtIndexPath:path];     }       break;     case UIGestureRecognizerStateChanged:       [self.collect updateInteractiveMovementTargetPosition:[gesture locationInView:gesture.view]];       break;     case UIGestureRecognizerStateEnded:       [self.collect endInteractiveMovement];       break;     default:       [self.collect cancelInteractiveMovement];       break;   } }  #pragma mark UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{   return self.dataSource.count; }  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{   return 1; }  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{   CustomCollectionCell * cell = (CustomCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];   UIImage *image = [UIImage imageNamed:[self.dataSource objectAtIndex:indexPath.row]];   [cell setImageWithText:image text:@""];      return cell; }  - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{   return YES; }  - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{      id item = [self.dataSource objectAtIndex:sourceIndexPath.item];   [self.dataSource removeObject:item];      [self.dataSource insertObject:item atIndex:destinationIndexPath.item]; }  #pragma mark UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{   TestViewController *test = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];   NSString *imageName = [self.dataSource objectAtIndex:indexPath.row];   test.imageName = imageName;      [self.navigationController pushViewController:test animated:YES]; }  - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{   return YES; }  //選中放大效果 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ //  CustomCollectionCell * cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; //   //  [UIView animateWithDuration:1 animations:^{ //    cell.transform = CGAffineTransformMakeScale(2.0f, 2.0f); //  }]; }  //縮小效果 - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ //  CustomCollectionCell * cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; //   //  [UIView animateWithDuration:1 animations:^{ //    cell.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //  }]; }   #pragma mark UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{      return CGSizeMake(100, 100); }  /*  * 上左下右間距  */ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{      return UIEdgeInsetsMake(15, 15, 15, 15); }  /*  * item space  */ - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{      return 8; }  /*  * 行距 20  */ - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{      return 10; } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久久久久久一区二区| 热久久免费国产视频| 亚洲成年网站在线观看| 日韩女优人人人人射在线视频| 日本久久精品视频| 深夜福利国产精品| 在线看欧美日韩| 欧美猛男性生活免费| 精品国产福利视频| 成人免费观看49www在线观看| 国产成人精品一区二区三区| 久久精品美女视频网站| 久久国内精品一国内精品| 91精品国产综合久久久久久久久| 亚洲欧美日韩一区二区三区在线| 日本久久亚洲电影| 亚洲视频国产视频| 亚洲**2019国产| 久青草国产97香蕉在线视频| 亚洲综合在线小说| 欧美成人合集magnet| 高潮白浆女日韩av免费看| 国产主播喷水一区二区| 日韩一区视频在线| 成人h片在线播放免费网站| 亚洲一级黄色片| 91系列在线观看| 欧美一级大片在线观看| 性色av一区二区三区在线观看| 久久精品国产v日韩v亚洲| 亚洲国产精品一区二区久| 国产久一一精品| 久久天天躁狠狠躁夜夜av| 69视频在线免费观看| 久久综合伊人77777| 亚洲www在线观看| 亚洲区在线播放| 日韩美女在线观看| 国产精品一区二区三区毛片淫片| 国产999在线观看| 日本欧美黄网站| 亚洲xxxxx电影| 欧美性生交大片免网| 国产精品久久久久影院日本| 亚洲精品v天堂中文字幕| 日韩电影中文 亚洲精品乱码| 亚洲成人激情在线观看| 亚洲精品国产福利| 麻豆精品精华液| 九色精品免费永久在线| 中文字幕在线精品| 日韩高清电影免费观看完整| 色偷偷av一区二区三区| 在线观看欧美日韩国产| 欧美疯狂做受xxxx高潮| 亚洲国产精久久久久久| xx视频.9999.com| 92版电视剧仙鹤神针在线观看| 成人网在线观看| 亚洲激情视频在线播放| 黑人狂躁日本妞一区二区三区| 亚洲精品综合精品自拍| 国产日韩av高清| 8x海外华人永久免费日韩内陆视频| 亚洲美女视频网| 日韩av在线免费| 91精品国产高清| 亚洲a区在线视频| 亚洲性视频网站| 91av在线国产| 久久久国产在线视频| 国产精品主播视频| 久久99久久99精品中文字幕| 中文日韩在线视频| 欧美黑人xxxx| 国产美女精品免费电影| 亚洲人成网在线播放| 成人妇女免费播放久久久| 国产综合福利在线| 久久伊人精品一区二区三区| 欧美在线观看视频| www.日韩不卡电影av| 色婷婷综合成人| 日韩精品在线视频观看| 国产日韩精品综合网站| 日韩av影视综合网| 午夜精品久久久久久99热| 日韩色av导航| 韩曰欧美视频免费观看| 国产日韩精品综合网站| 亚洲一区二区久久久久久| 欧美亚洲成人精品| 欧美午夜www高清视频| 精品国产999| 日韩中文字幕在线| 国产一区在线播放| 91色视频在线导航| 成人国产精品av| 日韩欧美亚洲范冰冰与中字| 国产欧美日韩免费| 欧美日韩不卡合集视频| 亚洲精品99久久久久中文字幕| 亚洲精选一区二区| 在线观看中文字幕亚洲| 海角国产乱辈乱精品视频| 久久成人精品视频| 美女精品视频一区| 国产日韩精品电影| 日韩av观看网址| 欧美日韩中文字幕在线视频| 福利视频一区二区| 国产精品91久久久久久| 亚洲第一区中文99精品| 中文字幕欧美日韩精品| 欧美极品美女电影一区| 在线观看久久av| 国产精品久久一区| 欧美成人午夜剧场免费观看| 国产日韩欧美影视| 精品中文字幕久久久久久| 麻豆国产精品va在线观看不卡| 欧美日韩中文字幕| 亚洲91精品在线观看| 久久99久久99精品免观看粉嫩| 午夜免费日韩视频| 日韩一区二区福利| 亚洲欧美日韩直播| 91av视频导航| 中文字幕精品视频| 欧美成人精品一区二区三区| 日韩福利在线播放| 亚洲国产天堂网精品网站| 国产精品91视频| 欧美一级在线亚洲天堂| 日韩精品高清在线| 欧美xxxx做受欧美| 午夜欧美大片免费观看| 久久69精品久久久久久国产越南| 亚洲人a成www在线影院| www.亚洲人.com| 亚洲精品v欧美精品v日韩精品| 国产成人精品久久久| 欧美综合激情网| 亚洲二区在线播放视频| 精品国偷自产在线视频| 国产999视频| 亚洲欧美精品一区二区| 国产精品18久久久久久麻辣| 色综合久久久久久中文网| 色综合色综合网色综合| 国产精品美女午夜av| 亚州av一区二区| 国产一区二区三区在线视频| 欧美做受高潮电影o| 高清亚洲成在人网站天堂| 日韩精品在线观看网站| 中文.日本.精品| 国产精品成人一区二区| 亚洲精品国产拍免费91在线| 日韩精品高清在线| 亚洲人成电影在线播放| 在线播放日韩专区| 亚洲色图校园春色| 久久久精品日本|