iOS 標(biāo)簽移動動畫

<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>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市麸澜,隨后出現(xiàn)的幾起案子挺尿,更是在濱河造成了極大的恐慌,老刑警劉巖炊邦,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件编矾,死亡現(xiàn)場離奇詭異,居然都是意外死亡馁害,警方通過查閱死者的電腦和手機(jī)窄俏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碘菜,“玉大人凹蜈,你說我怎么就攤上這事÷剑” “怎么了踪区?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長吊骤。 經(jīng)常有香客問我缎岗,道長,這世上最難降的妖魔是什么白粉? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任传泊,我火速辦了婚禮鼠渺,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘眷细。我一直安慰自己拦盹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布溪椎。 她就那樣靜靜地躺著普舆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪校读。 梳的紋絲不亂的頭發(fā)上沼侣,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音歉秫,去河邊找鬼蛾洛。 笑死,一個胖子當(dāng)著我的面吹牛雁芙,可吹牛的內(nèi)容都是我干的轧膘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼兔甘,長吁一口氣:“原來是場噩夢啊……” “哼谎碍!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起裂明,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤椿浓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后闽晦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扳碍,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年仙蛉,在試婚紗的時候發(fā)現(xiàn)自己被綠了笋敞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡荠瘪,死狀恐怖夯巷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情哀墓,我是刑警寧澤趁餐,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站篮绰,受9級特大地震影響后雷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一臀突、第九天 我趴在偏房一處隱蔽的房頂上張望勉抓。 院中可真熱鬧,春花似錦候学、人聲如沸藕筋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽隐圾。三九已至,卻和暖如春边翁,著一層夾襖步出監(jiān)牢的瞬間翎承,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工符匾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瘩例。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓啊胶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親垛贤。 傳聞我的和親對象是個殘疾皇子焰坪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容