iOS-關於自定義分段選擇器的一些小事(Segmented)

来源:https://www.cnblogs.com/wangkejia/archive/2019/07/09/11158051.html
-Advertisement-
Play Games

系統自帶的分段選擇就是 UISegmentedControl ,也有一些大佬自定義的 Segmented ,比如Git上的 HMSegmentedControl ,我以前最初的項目中,也有用到過,如果自己寫,或者想自定義一些UI,該從哪裡出發,其實在用過 HMSegmentedControl 之後, ...


系統自帶的分段選擇就是 UISegmentedControl ,也有一些大佬自定義的 Segmented ,比如Git上的 HMSegmentedControl ,我以前最初的項目中,也有用到過,如果自己寫,或者想自定義一些UI,該從哪裡出發,其實在用過 HMSegmentedControl 之後,大致就有思路了,如果想簡單的實現下,可以利用 UICollectionView 來實現,下麵是我利用 UICollectionView 寫的一個簡單的小慄子,效果圖

 

設計思路

 

首先利用 UICollectionView 處理每個item的大小,確切的說是寬度,那麼就要每次選中一個item後,重新計算所有的item的寬度,同時計算 UICollectionView 的 contentSize.width;

計算每個item寬度分為兩種情況,一種是選中的字體的顯示,一種是未選中的字體顯示,比如字體大小,顏色等,然後根據字體大小字元串長度,計算出字體需要展示的寬度,並計算對應的item寬度,最後把每個item的寬度保存起來,用於在 UICollectionView 代理方法中做處理;

計算 contentSize.width 由上圖可知,是由兩邊的間距,item之間的間距和每個item的總和,目的是利用 contentSize.width 計算下劃線的位置;

具體實現

#import "XKCollectionView.h"

///四周邊距
const static CGFloat _margin_left = 5;
const static CGFloat _margin_right = 5;
const static CGFloat _margin_top = 0;
const static CGFloat _margin_bottom = 2;
const static CGFloat _margin_space = 15;

const static CGFloat _line_width = 30.0;
const static CGFloat _line_height = 3.0;

@interface XKCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
///臨時數據
@property (nonatomic,strong) NSArray<NSString *> *titleArray;
///每個item的寬度
@property (nonatomic,strong) NSMutableArray *widthsArray;
///底部線條
@property (nonatomic,strong) UIView *lineView;
///選中的item索引
@property (nonatomic,assign) NSInteger selectIndex;
///選中的item string
@property (nonatomic,strong) NSString *selectString;
////計算出來的總寬度,用於設置 UICollectionView.contentSize.width
@property (nonatomic,assign) CGFloat totalContentWidth;
@end
@implementation XKCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)setup{
    _selectIndex = 0;
    self.widthsArray = [NSMutableArray array];
    [self addSubview:self.lineView];
    self.backgroundColor = [UIColor whiteColor];
    self.showsHorizontalScrollIndicator = NO;
    self.delegate = self;
    self.dataSource = self;
    [self registerClass:[XKCollectionViewCell class] forCellWithReuseIdentifier:@"XKCollectionViewCell"];
    
    _titleArray = @[@"一級建造師",@"二級建造師",@"造價工程師",@"咨詢工程師",@"註冊安全工程師",@"監理工程師",@"註冊電氣工程師",@"環境影響評價工程師",@"註冊城鄉規劃師",@"註冊消防工程師"];
    [self storeSegmentedWidth];
    [self reloadData];
    CGRect lineRext = [self measureLineFrame];
    self.lineView.frame = lineRext;
    ///設置偏移量
    [self setContentOffset:CGPointMake([self measureContentOffsetX], 0)];
}


- (void)updateSelectSeg{
    [self storeSegmentedWidth];
    [self reloadData];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect lineRext = [self measureLineFrame];
        self.lineView.frame = lineRext;
    }];
    
    [self setContentOffset:CGPointMake([self measureContentOffsetX], 0) animated:YES];
}
#pragma mark ========== 儲存計算好的item寬度 ==========
///每次切換時更新
- (void)storeSegmentedWidth{
    _selectIndex = 0;
    _totalContentWidth = 0;
    [self.widthsArray removeAllObjects];
    
    if (_selectString) {
        for (int i = 0; i < _titleArray.count; i ++) {
            NSString *title = _titleArray[i];
            if ([title isEqualToString:_selectString]) {
                _selectIndex = i;
                break;
            }
        }
    }
   
    
    for (int i = 0; i < _titleArray.count; i ++) {
        
        CGSize size = [self measureTitleIndex:i];
        NSNumber *value = [NSNumber numberWithFloat:size.width];
        [self.widthsArray addObject:value];
        
        _totalContentWidth = _totalContentWidth + size.width;
        if (i < _titleArray.count - 1) {
            _totalContentWidth = _totalContentWidth + _margin_space;
        }
    }
    _totalContentWidth = _totalContentWidth + _margin_left + _margin_right;
    
}
- (CGSize)measureTitleIndex:(NSUInteger)index {
    if (index >= _titleArray.count) {
        return CGSizeZero;
    }
    
    id title = _titleArray[index];
    CGSize size = CGSizeZero;
    BOOL selected = (index == _selectIndex);
    NSDictionary *titleAttrs = selected ? [self resultingSelectedTitleTextAttributes] : [self resultingTitleTextAttributes];
    size = [(NSString *)title sizeWithAttributes:titleAttrs];
    UIFont *font = titleAttrs[@"NSFont"];
    size = CGSizeMake(ceil(size.width), ceil(size.height - font.descender));
    CGSize resault = CGRectIntegral((CGRect){CGPointZero, size}).size;
    return resault;
}
- (NSDictionary *)resultingSelectedTitleTextAttributes {
    NSDictionary *resultingAttrs = @{NSForegroundColorAttributeName : [UIColor blackColor] ,NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:18.0]};
    return resultingAttrs;
}
- (NSDictionary *)resultingTitleTextAttributes {
    NSDictionary *resultingAttrs = @{NSForegroundColorAttributeName : [UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:14.0]};
    return resultingAttrs;
}
#pragma mark ========== 計算下劃線位置 ==========
- (CGRect)measureLineFrame{
    CGRect lineRect = CGRectZero;
    CGFloat lineRectX = 0;
    for (int i = 0; i < _selectIndex; i ++) {
        NSNumber *number = self.widthsArray[i];
        lineRectX = lineRectX + [number floatValue] + _margin_space;
    }
    CGFloat widthSelect = [self.widthsArray[_selectIndex] floatValue];
    CGFloat lastLocation = widthSelect >= _line_width ? (widthSelect - _line_width)/2 : (_line_width - widthSelect)/2;
    lineRectX = lineRectX + _margin_left + lastLocation;
    
    lineRect = CGRectMake(lineRectX, self.bounds.size.height - _line_height - 2, _line_width, _line_height);
    return lineRect;
}
#pragma mark ========== 計算偏移量 ==========
- (CGFloat)measureContentOffsetX{
    CGFloat selfWidth = self.bounds.size.width;
    
    ///先計算點擊的item中心點
    CGFloat selectedCenterX = 0;
    for (int i = 0; i < _selectIndex; i ++) {
        NSNumber *number = self.widthsArray[i];
        selectedCenterX = selectedCenterX + [number floatValue] + _margin_space;
    }
    CGFloat widthSelect = [self.widthsArray[_selectIndex] floatValue];
    selectedCenterX = selectedCenterX + widthSelect/2;
    
    if (_totalContentWidth <= selfWidth) {///充滿內部不做偏移
        return 0;
    }
    
    if (selectedCenterX <= selfWidth/2) {
        return 0;
    }
    else if (selectedCenterX >= _totalContentWidth - selfWidth/2){
        return _totalContentWidth - selfWidth;
    }
    else{
        return selectedCenterX - selfWidth/2;
    }
}
#pragma mark ========== 代理 ==========
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _titleArray.count;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return _margin_space;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(_margin_top, _margin_left, _margin_bottom, _margin_right);
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *number = self.widthsArray[indexPath.row];
    return CGSizeMake([number floatValue],30);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XKCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XKCollectionViewCell" forIndexPath:indexPath];
    NSString *title = _titleArray[indexPath.row];
    cell.title = title;
    if (indexPath.row == _selectIndex) {
        cell.isSelectd = YES;
    }
    else{
        cell.isSelectd = NO;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _selectString = _titleArray[indexPath.row];
    [self updateSelectSeg];
}
#pragma mark ========== 變數 ==========

- (UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor purpleColor];
        _lineView.layer.masksToBounds = YES;
        _lineView.layer.cornerRadius = _line_height/2;
    }
    return _lineView;
}

@end

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、文檔 1)ElasticSearch是面向文檔的,文檔是所有可搜索數據的最小單位。例如: a)日誌文件中的日誌項; b)一張唱片的詳細信息; c)一篇文章中的具體內容; 2)在ElasticSearch中,文檔會被序列化成Json格式: a)Json對象是由欄位組成的; b)每個欄位都有對應的字 ...
  • [TOC] 一、資料庫的安裝和連接 1.1 PyMySQL的安裝 1.2 python連接資料庫 1.2.1 更多參數版 二、創建表操作 三、操作數據 3.1 插入操作 3.2 查詢操作 Python查詢Mysql使用 fetchone() 方法獲取單條數據,使用 :方法獲取多條數據。 : 該方法獲 ...
  • Redis主從複製機制詳解 Redis有兩種不同的持久化方式,Redis伺服器通過持久化,把Redis記憶體中持久化到硬碟當中,當Redis宕機時,我們重啟Redis伺服器時,可以由RDB文件或AOF文件恢復記憶體中的數據。 不過持久化後的數據仍然只在一臺機器上,因此當硬體發生故障時,比如主板或CPU壞 ...
  • 第0步:版本選擇 AOSP版本選擇很重要,如果選錯了,會造成編譯失敗等各種問題,編譯AOSP對Xcode的版本是有要求的; 比如:AOSP6.0 7.0,要求Xcode的版本是8.3,然而在MacOS 10.14上面是不支持Xcode8.3的這就很尷尬; 由於現在大家的Mac環境基本是更新到最新的1 ...
  • 在Dart裡面,變數的聲明使用var、Object或Dynamic關鍵字,如下所示: var name = ‘張三’; 在Dart語言里一切皆為對象,所以如果沒有將變數初始化,那麼它的預設值為null(包括數字),如果需要判斷變數是否為null,則需要進行如下判斷: String name;if(n... ...
  • iOS查看屏幕幀數工具--YYFPSLabel iOS 保持界面流暢的技巧 iOS 優化界面流暢度的探討 先研究一下 改天自己出一篇文章 ...
  • 原文:https://www.jianshu.com/p/b5a484cecd7c 本文主要說明2018年蘋果開發者賬號申請的流程,申請流程相較於2017年有一些改變,希望大家能夠通過本文少走一些彎路,能夠順利完成開發者賬號的申請。關於新流程中可能出現的一些問題以及部分流程的變更均在下文中運用灰色塊 ...
  • 一、數據綁定 二、列表渲染 <!--index.wxml--> 三、綁定一個點擊事件 四、事件冒泡 五、事件傳參 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...