ZWDragSortView
自適應標簽拖拽排序
功能
適用于有標簽拖拽自動排序需求的人使用,該封裝最大的特點是自適應,只要外部傳入需要顯示的標簽數組,就可以自動計算該自定義view的高度,不會有多余的空白。當顯示的標簽數量很大,顯示高度超過屏幕,可以支持滾動。可以手動添加標簽數量,點擊標簽支持刪除,高度自適應不用額外處理。
文件結構
該封裝只有ZWDragSortView.h和ZWDragSortView.m兩個文件。
ZWDragSortView.h頭文件定義如下:
typedef void(^ZWClickBtnBlock)(NSInteger index,NSString * title);
@interface ZWDragSortView : UIScrollView
@property (nonatomic,strong) NSMutableArray * titlesArr;//按鈕顯示數組
@property (nonatomic,assign) CGFloat columnMargin;//按鈕間隔
@property (nonatomic,assign) NSInteger columnCount;//按鈕列數
@property (nonatomic,strong) UIColor * titlesColor;//設置按鈕文字顏色
@property (nonatomic,strong) UIColor * btnBackgroundColor;//設置按鈕背景顏色
@property (nonatomic,assign) CGFloat tagHeight;//設置按鈕標簽高度
@property (nonatomic,strong) UIFont * titleFont;//設置按鈕文字字體大小
@property (nonatomic,assign) CGFloat viewY;//直接設置控件的y值即可食用
@property (nonatomic,assign) BOOL isDragEnable;//長按拖拽使能,默認開啟
//初始化
+(instancetype)sortViewWithTitles:(NSMutableArray*)titles;
//點擊標簽回調
-(void)clickBtnActionBack:(ZWClickBtnBlock)block;
titlesArr屬性用于標簽變化的時候重新布局使用;支持自定義按鈕列數,按鈕間隔,不賦值使用默認值;支持按鈕文字顏色、字體大小,控件背景色設置,支持長按拖拽功能關閉,外部傳入viewY值可以改變控件豎直方向位置,也可以自定義控件frame。
用法
引入ZWDragSortView.h頭文件,
self.datas = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
[self.datas addObject:[NSString stringWithFormat:@"%02ld",i]];
}
ZWDragSortView * view = [ZWDragSortView sortViewWithTitles:self.datas ];
[view clickBtnActionBack:^(NSInteger index, NSString *title) {
[self.datas removeObjectAtIndex:index];
self.cutomView.titlesArr = self.datas;
}];
//view.viewY = 350;
//view.frame = CGRectMake(30, 30, 230, 200);
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
self.cutomView = view;
外部傳入一個字符串數組,用類方法sortViewWithTitles:創建對象,可以設置各種參數,也可以不設置,直接addSubview。clickBtnActionBack方法用于點擊標簽回調事件結果,沒有點擊標簽需求可以忽略。viewY用于設置控件的位置,默認y值是64,寬度是屏幕寬度,也可以自定義控件寬高。
演示效果
demo演示.gif