iOS 标签移动动画

2017-03-28  本文已影响180人  萧城x

<pre>
CGFloat const imageViewWH = 20;

@interface YZTagList ()
{
NSMutableArray *_tagArray;
}
@property (nonatomic, weak) UICollectionView *tagListView;
@property (nonatomic, strong) NSMutableDictionary tags;
@property (nonatomic, strong) NSMutableArray tagButtons;
/

@implementation YZTagList

pragma mark - 初始化

pragma mark - 操作标签方法

// 添加多个标签

// 点击标签

// 拖动标签

// 改变
if (pan.state == UIGestureRecognizerStateChanged) {
    
    // 获取当前按钮中心点在哪个按钮上
    UIButton *otherButton = [self buttonCenterInButtons:tagButton];
    
    if (otherButton) { // 插入到当前按钮的位置
        // 获取插入的角标
        NSInteger i = otherButton.tag;
        
        // 获取当前角标
        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) { // 往前插
            
            // 更新之后标签frame
            [UIView animateWithDuration:0.25 animations:^{
                [self updateLaterTagButtonFrame:i + 1];
            }];
            
        } else { // 往后插
            
            // 更新之前标签frame
            [UIView animateWithDuration:0.25 animations:^{
                [self updateBeforeTagButtonFrame:i];
            }];
        }
    }
    
}

// 结束
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];

}

// 更新标签

// 更新之前按钮

// 更新以后按钮

// 获取当前按钮
YZTagButton *tagButton = self.tagButtons[i];
// 判断是否设置标签的尺寸
if (_tagSize.width == 0) { // 没有设置标签尺寸
    // 自适应标签尺寸
    // 设置标签按钮frame(自适应)
    [self setupTagButtonCustomFrame:tagButton preButton:preButton extreMargin:extreMargin];
} else { // 按规律排布
    // 计算标签按钮frame(regular)
    [self setupTagButtonRegularFrame:tagButton];
}

}

// 计算标签按钮frame(按规律排布)

// 设置标签按钮frame(自适应)

</pre>

上一篇 下一篇

猜你喜欢

热点阅读