<pre>
CGFloat const imageViewWH = 20;
@interface YZTagList ()
{
NSMutableArray *_tagArray;
}
@property (nonatomic, weak) UICollectionView *tagListView;
@property (nonatomic, strong) NSMutableDictionary tags;
@property (nonatomic, strong) NSMutableArray tagButtons;
/
- 需要移動的矩陣
*/
@property (nonatomic, assign) CGRect moveFinalRect;
@property (nonatomic, assign) CGPoint oriCenter;
@end
@implementation YZTagList
(NSMutableArray *)tagArray
{
if (_tagArray == nil) {
_tagArray = [NSMutableArray array];
}
return _tagArray;
}(NSMutableArray *)tagButtons
{
if (_tagButtons == nil) {
_tagButtons = [NSMutableArray array];
}
return _tagButtons;
}(NSMutableDictionary *)tags
{
if (_tags == nil) {
_tags = [NSMutableDictionary dictionary];
}
return _tags;
}-
(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}return self;
}
pragma mark - 初始化
(void)setup
{
_tagMargin = 10;
_tagColor = [UIColor redColor];
_tagButtonMargin = 5;
_tagCornerRadius = 5;
_borderWidth = 0;
_borderColor = _tagColor;
_tagListCols = 4;
_scaleTagInSort = 1;
_isFitTagListH = YES;
_tagFont = [UIFont systemFontOfSize:13];
self.clipsToBounds = YES;
}-
(void)layoutSubviews
{
[super layoutSubviews];_tagListView.frame = self.bounds;
} (void)setScaleTagInSort:(CGFloat)scaleTagInSort
{
if (_scaleTagInSort < 1) {
@throw [NSException exceptionWithName:@"YZError" reason:@"(scaleTagInSort)縮放比例必須大于1" userInfo:nil];
}
_scaleTagInSort = scaleTagInSort;
}(CGFloat)tagListH
{
if (self.tagButtons.count <= 0) return 0;
return CGRectGetMaxY([self.tagButtons.lastObject frame]) + _tagMargin;
}
pragma mark - 操作標(biāo)簽方法
// 添加多個標(biāo)簽
-
(void)addTags:(NSArray *)tagStrs
{
if (self.frame.size.width == 0) {
@throw [NSException exceptionWithName:@"YZError" reason:@"先設(shè)置標(biāo)簽列表的frame" userInfo:nil];
}for (NSString *tagStr in tagStrs) {
[self addTag:tagStr];
}
}
// 添加標(biāo)簽 -
(void)addTag:(NSString *)tagStr
{
Class tagClass = _tagClass?_tagClass : [YZTagButton class];// 創(chuàng)建標(biāo)簽按鈕
YZTagButton *tagButton = [tagClass buttonWithType:UIButtonTypeCustom];
if (_tagClass == nil) {
tagButton.margin = _tagButtonMargin;
}
tagButton.layer.cornerRadius = _tagCornerRadius;
tagButton.layer.borderWidth = _borderWidth;
tagButton.layer.borderColor = _borderColor.CGColor;
tagButton.clipsToBounds = YES;
tagButton.tag = self.tagButtons.count;
[tagButton setImage:_tagDeleteimage forState:UIControlStateNormal];
[tagButton setTitle:tagStr forState:UIControlStateNormal];
[tagButton setTitleColor:_tagColor forState:UIControlStateNormal];
[tagButton setBackgroundColor:_tagBackgroundColor];
[tagButton setBackgroundImage:_tagBackgroundImage forState:UIControlStateNormal];
tagButton.titleLabel.font = _tagFont;
[tagButton addTarget:self action:@selector(clickTag:) forControlEvents:UIControlEventTouchUpInside];
if (_isSort) {
// 添加拖動手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[tagButton addGestureRecognizer:pan];
}
[self addSubview:tagButton];// 保存到數(shù)組
[self.tagButtons addObject:tagButton];// 保存到字典
[self.tags setObject:tagButton forKey:tagStr];
[self.tagArray addObject:tagStr];// 設(shè)置按鈕的位置
[self updateTagButtonFrame:tagButton.tag extreMargin:YES];// 更新自己的高度
if (_isFitTagListH) {
CGRect frame = self.frame;
frame.size.height = self.tagListH;
[UIView animateWithDuration:0.25 animations:^{
self.frame = frame;
}];
}
}
// 點(diǎn)擊標(biāo)簽
-
(void)clickTag:(UIButton *)button
{if (_clickTagBlock) {
_clickTagBlock(button.currentTitle);
}
}
// 拖動標(biāo)簽
-
(void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取偏移量
CGPoint transP = [pan translationInView:self];UIButton *tagButton = (UIButton *)pan.view;
// 開始
if (pan.state == UIGestureRecognizerStateBegan) {
_oriCenter = tagButton.center;
[UIView animateWithDuration:-.25 animations:^{
tagButton.transform = CGAffineTransformMakeScale(_scaleTagInSort, _scaleTagInSort);
}];
[self addSubview:tagButton];
}CGPoint center = tagButton.center;
center.x += transP.x;
center.y += transP.y;
tagButton.center = center;
// 改變
if (pan.state == UIGestureRecognizerStateChanged) {
// 獲取當(dāng)前按鈕中心點(diǎn)在哪個按鈕上
UIButton *otherButton = [self buttonCenterInButtons:tagButton];
if (otherButton) { // 插入到當(dāng)前按鈕的位置
// 獲取插入的角標(biāo)
NSInteger i = otherButton.tag;
// 獲取當(dāng)前角標(biāo)
NSInteger curI = tagButton.tag;
_moveFinalRect = otherButton.frame;
// 排序
// 移除之前的按鈕
[self.tagButtons removeObject:tagButton];
[self.tagButtons insertObject:tagButton atIndex:i];
[self.tagArray removeObject:tagButton.currentTitle];
[self.tagArray insertObject:tagButton.currentTitle atIndex:i];
// 更新tag
[self updateTag];
if (curI > i) { // 往前插
// 更新之后標(biāo)簽frame
[UIView animateWithDuration:0.25 animations:^{
[self updateLaterTagButtonFrame:i + 1];
}];
} else { // 往后插
// 更新之前標(biāo)簽frame
[UIView animateWithDuration:0.25 animations:^{
[self updateBeforeTagButtonFrame:i];
}];
}
}
}
// 結(jié)束
if (pan.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.25 animations:^{
tagButton.transform = CGAffineTransformIdentity;
if (_moveFinalRect.size.width <= 0) {
tagButton.center = _oriCenter;
} else {
tagButton.frame = _moveFinalRect;
}
} completion:^(BOOL finished) {
_moveFinalRect = CGRectZero;
}];
}
[pan setTranslation:CGPointZero inView:self];
}
// 更新標(biāo)簽
- (void)updateTag
{
NSInteger count = self.tagButtons.count;
for (int i = 0; i < count; i++) {
UIButton *tagButton = self.tagButtons[i];
tagButton.tag = i;
}
}
// 更新之前按鈕
- (void)updateBeforeTagButtonFrame:(NSInteger)beforeI
{
for (int i = 0; i < beforeI; i++) {
// 更新按鈕
[self updateTagButtonFrame:i extreMargin:NO];
}
}
// 更新以后按鈕
-
(void)updateLaterTagButtonFrame:(NSInteger)laterI
{
NSInteger count = self.tagButtons.count;for (NSInteger i = laterI; i < count; i++) {
// 更新按鈕
[self updateTagButtonFrame:i extreMargin:NO];
}
} -
(void)updateTagButtonFrame:(NSInteger)i extreMargin:(BOOL)extreMargin
{
// 獲取上一個按鈕
NSInteger preI = i - 1;// 定義上一個按鈕
UIButton *preButton;// 過濾上一個角標(biāo)
if (preI >= 0) {
preButton = self.tagButtons[preI];
}
// 獲取當(dāng)前按鈕
YZTagButton *tagButton = self.tagButtons[i];
// 判斷是否設(shè)置標(biāo)簽的尺寸
if (_tagSize.width == 0) { // 沒有設(shè)置標(biāo)簽尺寸
// 自適應(yīng)標(biāo)簽尺寸
// 設(shè)置標(biāo)簽按鈕frame(自適應(yīng))
[self setupTagButtonCustomFrame:tagButton preButton:preButton extreMargin:extreMargin];
} else { // 按規(guī)律排布
// 計算標(biāo)簽按鈕frame(regular)
[self setupTagButtonRegularFrame:tagButton];
}
}
// 計算標(biāo)簽按鈕frame(按規(guī)律排布)
- (void)setupTagButtonRegularFrame:(UIButton *)tagButton
{
// 獲取角標(biāo)
NSInteger i = tagButton.tag;
NSInteger col = i % _tagListCols;
NSInteger row = i / _tagListCols;
CGFloat btnW = _tagSize.width;
CGFloat btnH = _tagSize.height;
NSInteger margin = (self.bounds.size.width - _tagListCols * btnW - 2 * _tagMargin) / (_tagListCols - 1);
CGFloat btnX = _tagMargin + col * (btnW + margin);;
CGFloat btnY = _tagMargin + row * (btnH + margin);
tagButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
// 設(shè)置標(biāo)簽按鈕frame(自適應(yīng))
-
(void)setupTagButtonCustomFrame:(UIButton *)tagButton preButton:(UIButton *)preButton extreMargin:(BOOL)extreMargin
{
// 等于上一個按鈕的最大X + 間距
CGFloat btnX = CGRectGetMaxX(preButton.frame) + _tagMargin;// 等于上一個按鈕的Y值,如果沒有就是標(biāo)簽間距
CGFloat btnY = preButton? preButton.frame.origin.y : _tagMargin;// 獲取按鈕寬度
CGFloat titleW = [tagButton.titleLabel.text sizeWithFont:_tagFont].width;
CGFloat titleH = [tagButton.titleLabel.text sizeWithFont:_tagFont].height;
CGFloat btnW = extreMargin?titleW + 2 * _tagButtonMargin : tagButton.bounds.size.width ;
if (_tagDeleteimage && extreMargin == YES) {
btnW += imageViewWH;
btnW += _tagButtonMargin;
}// 獲取按鈕高度
CGFloat btnH = extreMargin? titleH + 2 * _tagButtonMargin:tagButton.bounds.size.height;
if (_tagDeleteimage && extreMargin == YES) {
CGFloat height = imageViewWH > titleH ? imageViewWH : titleH;
btnH = height + 2 * _tagButtonMargin;
}// 判斷當(dāng)前按鈕是否足夠顯示
CGFloat rightWidth = self.bounds.size.width - btnX;if (rightWidth < btnW) {
// 不夠顯示敞临,顯示到下一行
btnX = _tagMargin;
btnY = CGRectGetMaxY(preButton.frame) + _tagMargin;
}tagButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
</pre>