国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

ios的collection控件的自定義布局實(shí)現(xiàn)與設(shè)計(jì)

2020-07-26 02:36:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

collection控件用來(lái)實(shí)現(xiàn)界面的各種自定義布局,最常用其作為橫向、豎向的布局控件。很早之前,系統(tǒng)對(duì)于collection的支持并不是很好。所以自己實(shí)現(xiàn)了支持自定義布局、自定義cell的collection控件。自定義的collection可以滿足所有的產(chǎn)品特殊需求及動(dòng)態(tài)效果,例如在某些特殊情況下可能需要除選中cell之外的其它c(diǎn)ell執(zhí)行布局動(dòng)畫(huà)等。在collection的基礎(chǔ)之上,我又實(shí)現(xiàn)了支持cell拖動(dòng)、拖離窗體的tabview控件。本文主要介紹自定義collection的設(shè)計(jì)與實(shí)現(xiàn),后續(xù)持續(xù)更新多tab的tabview控件。

我有幾張阿里云幸運(yùn)券分享給你,用券購(gòu)買或者升級(jí)阿里云相應(yīng)產(chǎn)品會(huì)有特惠驚喜哦!把想要買的產(chǎn)品的幸運(yùn)券都領(lǐng)走吧!快下手,馬上就要搶光了。

產(chǎn)品中的一些實(shí)現(xiàn)效果

mac旺旺表情面板,實(shí)現(xiàn)grid與橫向布局

mac千牛工作臺(tái)用作橫向布局

iOS千牛歷史登錄頁(yè)面實(shí)現(xiàn)當(dāng)前選中cell變大并且選中cell總中最中位置校準(zhǔn)動(dòng)效的效果

collection

collection主要包括:繼承scrollview的collectionView,數(shù)據(jù)源協(xié)議collectionViewDataSource,事件響應(yīng)協(xié)議collectoinViewDelegate,布局基類collectoinLayout以及展示單元collectionCellView。

模塊圖如下:

 collectionView

collection容器包含指實(shí)現(xiàn)collectionViewDataSource、collectoinViewDelegate協(xié)議的指針以及collectoinLayout成員,同時(shí)維護(hù)collectoinCellView的控件重用。

@interface WWCollectionView : NSScrollView// 布局對(duì)象@property (retain) WWCollectionViewLayout *layout;// 數(shù)據(jù)源@property (weak) id dataSource;// 事件響應(yīng)@property (weak) id delegate;// 重加載數(shù)據(jù)(void)reloadData;// 重排布(void)invalidateLayout;// 取消返回選中(void)unSelectedAll;// 注冊(cè)重用對(duì)象(void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;// 對(duì)象重用(id)dequeueReusableCellWithReuseIdentifier:(NSString )identifier forIndexPath:(NSIndexPath )indexPath;// 設(shè)置選中對(duì)象(void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;// 當(dāng)前選中對(duì)象(NSIndexPath *)selectedItem;// 重加載indexPath item(void)reloadItemsAtIndexPath:(NSIndexPath *)indexPath;// 插入(void)insertItemsAtIndexPath:(NSIndexPath *)indexPath withAnimate:(BOOL)animate;// 刪除(void)deleteItemsAtIndexPath:(NSIndexPath *)indexPath withAnimate:(BOOL)animate;// 重新排列(void)relayoutWithAnimation:(BOOL)animated completion:(void (^)(BOOL finished))completion;// 滾動(dòng)到aPoint(void)scrollToPoint:(NSPoint)aPoint withAnimate:(BOOL)animate;@end

collectionViewDataSource

collection展示的數(shù)據(jù)源,由宿主實(shí)現(xiàn)。

@protocol WWCollectionViewDataSource // 返回indexPath下標(biāo)的cell(WWCollectionCellView )collectView:(WWCollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;// 總cell個(gè)數(shù)(NSInteger)numberOfItemInCollectionView:(WWCollectionView *)collectionView;// cell的數(shù)據(jù)(id)collectionView:(WWCollectionView )colletionView objectValueAtIndexPath:(NSIndexPath )indexPath;@end

collectoinViewDelegate

collection事件的回調(diào)響應(yīng),由宿主實(shí)現(xiàn)。

@protocol WWCollectionViewDelegate // indexPath元素被選中(void)collectionView:(WWCollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath;// 是否支持選中(BOOL)collectionView:(WWCollectionView )collectionView shouldSelectItemsAtIndexPaths:(NSIndexPath )indexPath;@end

collectoinLayout

collectionCellView的布局方案。

@interface WWCollectionViewLayout : NSObject// 布局基類@property (weak) WWCollectionView *collectionView;// 每個(gè)cell元素大小@property (assign) NSSize itemSize;// edgeInsets@property (assign) NSEdgeInsets edgeInsets;// scrollview使用,表示整個(gè)畫(huà)布大小@property (assign) NSSize viewContentSize;(instancetype)initWithCollectionView:(WWCollectionView *)collectionView;(void)invalidateLayout;// 返回index的cell大小(NSRect)frameForIndexPath:(NSIndexPath *)index total:(NSInteger)total;(NSSize)collectionViewContentSize;@end// 橫向布局控件@interface WWFlowCollectionViewLayout : WWCollectionViewLayout@property (assign) CGFloat headMargin;@property (assign) CGFloat tailMargin;@end// grid布局控件@interface WWGridCollectionViewLayout : WWCollectionViewLayout// 每行多少個(gè)@property (assign) NSInteger numberPerRow;@property (assign) CGFloat headMargin;@property (assign) CGFloat tailMargin;@end
@implementation WWFlowCollectionViewLayout
(void)invalidateLayout {NSInteger cellCount = [self.collectionView.dataSource numberOfItemInCollectionView:self.collectionView];CGRect bounds = self.collectionView.bounds;// 畫(huà)布寬度CGFloat width = _headMargin + _tailMargin + (cellCount - 1) (self.edgeInsets.left + self.edgeInsets.right) + self.itemSize.width cellCount;if (width < bounds.size.width) {width = bounds.size.width;}self.viewContentSize = NSMakeSize(width, bounds.size.height);[super invalidateLayout];}(NSRect)frameForIndexPath:(NSIndexPath *)index total:(NSInteger)total {CGFloat leftPos = self.headMargin + [index indexAtPosition:0] * (self.itemSize.width + self.edgeInsets.left + self.edgeInsets.right);// 返回cell的rectreturn NSMakeRect(leftPos, self.edgeInsets.top, self.itemSize.width, self.itemSize.height);}@end

collectoinCellView

collection展示的cell控件。

@interface WWCollectionCellView : NSView// 當(dāng)前cell被選中@property (nonatomic, assign) BOOL selected;// 數(shù)據(jù)@property (nonatomic, retain) id dataValue;// 使用前重置展示效果(void)reset;@end
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蕲春县| 高台县| 赤城县| 鹰潭市| 海林市| 南乐县| 喜德县| 资阳市| 道孚县| 江西省| 安徽省| 合水县| 新蔡县| 井研县| 郸城县| 吉木乃县| 白水县| 闵行区| 宁强县| 吴桥县| 宽城| 邳州市| 安乡县| 视频| 巴中市| 新郑市| 库车县| 宣武区| 长宁县| 大洼县| 高阳县| 青海省| 中方县| 平原县| 大邑县| 山东| 平乐县| 江阴市| 桑日县| 望都县| 绥芬河市|