多个按钮弹出动画

前言

说到APP用户体验,就离不开动画。在这篇文章里,简单实现了点击一个按钮弹出多个按钮的动画,在此抛砖引玉,供大家参考。

思路

首先创建需要被弹出的多个按钮,然后创建点击弹出的POP按钮,将其覆盖在多个按钮之上,最后在POP按钮点击事件里边利用UIView动画和CABasicAnimation动画,就可以实现简单的弹出效果。


按钮层次结构图.png

主要代码

创建UI,这里使用数组来装多个按钮,方便后面取用。

@interface ViewController ()
/**弹出按钮 */
@property (nonatomic,strong) UIButton  *popButton;
/**按钮数组*/
@property (nonatomic,strong) NSMutableArray *buttonArray;
@end

@implementation ViewController

- (UIButton *) popButton
{
    if (_popButton == nil)
    {
        _popButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.centerX - 30, self.view.centerY - 30 + 180, 60, 60)];
       
        [self.view addSubview:_popButton];
        [_popButton addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _popButton;
}
- (NSMutableArray *) buttonArray
{
    if (_buttonArray==nil)
    {
        _buttonArray=[[NSMutableArray alloc]init];
    }
    return _buttonArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //注意顺序
    [self initUI];
    self.popButton.backgroundColor = [UIColor grayColor];

    

}
- (void)initUI
{
    //循环创建按钮
    for (NSInteger i = 0; i<5; i++)
    {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.centerX - 30,self.view.centerY - 30 + 180, 60, 60)];
        //标记tag
        button.tag = 100+i;
        button.backgroundColor = [UIColor orangeColor];
        NSString *string = [NSString stringWithFormat:@"%ld",i];
        [button setTitle:string forState:UIControlStateNormal];
        button.layer.cornerRadius = 30;
        [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [self.buttonArray addObject:button];
    }
    
}

在POP按钮点击事件中,根据按钮的选中状态来判断是弹出还是收回多个按钮。这里使用的适配,是自己写的一个UIView的分类,参考代码适配常用分类

/**
 按钮点击响应
 */
- (void)popAction:(UIButton *)button
{
    if (button.selected == NO)
    {
        //弹出
        [self show];
        button.selected = YES;
    }
    else
    {  
        //消失
        [self dismiss];
        button.selected = NO;
    }
}

弹出多个按钮。这里需要注意的是,弹出按钮,只是一个动画效果,按钮本身的位置还是没有改变,需要我们在动画结束后,重新设置按钮的位置。

/**
 弹出
 */
- (void)show
{
    //旋转弹出按钮
    [UIView animateWithDuration:popTime animations:^{
       self.popButton.transform = CGAffineTransformMakeRotation(M_PI_2/2);
    }];
    
    //取出按钮,添加动画
    [self.buttonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //位移动画
        UIButton *button = self.buttonArray[idx];
        CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
        anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(_popButton.centerX, _popButton.centerY)];
        anima.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.centerX, self.view.centerY + 80 * idx - 250)];
        anima.duration = popTime;
        anima.fillMode = kCAFillModeForwards;
        anima.removedOnCompletion = NO;
        anima.beginTime = CACurrentMediaTime() + 0.02 * idx;
        [button.layer addAnimation:anima forKey:nil];
        //缩放动画
        CABasicAnimation *scaleAnima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnima.duration = popTime;
        scaleAnima.fromValue = @0;
        scaleAnima.toValue = @1;
        scaleAnima.fillMode = kCAFillModeForwards ;
        scaleAnima.removedOnCompletion = NO ;
        scaleAnima.beginTime = CACurrentMediaTime() + 0.02 * idx;
        [button.layer addAnimation:scaleAnima forKey:nil];
        //等动画结束后,改变按钮真正位置
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(popTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            button.center = CGPointMake(self.view.centerX,  self.view.centerY + 80 * idx - 250);
        });
    }];
}

隐藏弹出的多个按钮

/**
 隐藏
 */
- (void)dismiss
{
    [UIView animateWithDuration:popTime animations:^{
        self.popButton.transform = CGAffineTransformMakeRotation(0);
    }];

    [self.buttonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIButton *button = self.buttonArray[idx];
        CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
        anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.view.centerX, self.view.centerY + 80 * idx - 250)];
        anima.toValue = [NSValue valueWithCGPoint:CGPointMake(_popButton.centerX, _popButton.centerY)];
        anima.duration = popTime;
        anima.fillMode = kCAFillModeForwards;
        anima.removedOnCompletion = NO;
        anima.beginTime = CACurrentMediaTime() + 0.02 * (4 - idx);
        [button.layer addAnimation:anima forKey:nil];

        CABasicAnimation *scaleAnima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnima.duration = popTime;
        scaleAnima.fromValue = @1;
        scaleAnima.toValue = @0;
        scaleAnima.fillMode = kCAFillModeForwards ;
        scaleAnima.removedOnCompletion = NO ;
        scaleAnima.beginTime = CACurrentMediaTime() + 0.02 * (4 - idx);
        [button.layer addAnimation:scaleAnima forKey:nil];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(popTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            button.center = CGPointMake(_popButton.centerX,  _popButton.centerY);
        });
 
    }];
}

效果图

效果图.gif

Demo地址

https://github.com/JmoVxia/ButtonPopAnimationDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 1500块钱的莱尔斯丹你不要,非要买四五百块的百丽;八九百块钱的阿迪你不要,非要买双两三百的外贸原单;你用一周的时...
    何初见阅读 449评论 0 4
  • 文/蒹葭 生命的长度如同沙漏,慢慢流逝,一寸寸减少。可有时候我们会打碎别人的沙漏…… 2017/08/29 星期...
    一只蒹葭阅读 407评论 20 15
  • 索引:今天是孔子诞辰2567周年,转录几年前涂鸦之作《我们怎样当老师》,聊表敬意。 读《论语》,读《史记·孔子世家...
    东丰林波阅读 770评论 0 0
  • 我叫小草帽。今年19岁。大二。沉溺于各种想法中无法解脱,患有孤独综合症,独行侠。因此想写下一些心路历程,以便回顾和...
    小草帽_阅读 356评论 0 1