移动互联网技术分享

btn_AnimationShow(按钮 简易弹出收复动画)

2016-05-05  本文已影响203人  littleDad
动画效果.gif
#define centerX _currentBtn.frame.origin.x //_currentBtn 的X
#define centerY _currentBtn.frame.origin.y //_currentBtn 的Y
#define btnWHalf btnW / 2
#define adjushY  20.
#define space 10.  //子btn 间距
#define margin 20. //子、父btn 距离
#define btnW 20.
#define btnCount 5
#define LeftCount 2

写的有点 匆忙 ,没有单独封装到UIView上,所以大家 将就 用宏代替接口吧!!!哈哈哈

centerY和centerX来设置 父 按钮 origin的x和y,
通过改变它 会改变 所有子 按钮originx和y。btnCount是子 按钮的总数,LeftCount则是影响水平子按钮左右显示的数量。

typedef void (^createPointLeft)(BOOL, NSInteger index);
typedef void (^createPointRight)(BOOL, NSInteger index);
定义两个没有返回值的block用来保存要重用的代码,左弹出 、右弹出的动画效果我们是要在水平弹出中复用的。

@property (nonatomic, assign)NSTimeInterval currentTime;用来保存每次执行动画前的当前时间,以便确定每个按钮动画执行的时机。

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(((0.5/(float)_btnMarr.count * (float)_btnMarr.count )) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _currentBtn.enabled = YES;
    });
self.pointLeft = ^(BOOL sure, NSInteger index){
        startPoint = _currentBtn.isSelected ? CGPointMake(centerX -btnWHalf, centerY +adjushY - btnWHalf) : CGPointMake(centerX -btnWHalf - index * (btnW + space) - margin, centerY +adjushY - btnWHalf);
        endPoint = _currentBtn.isSelected ? CGPointMake(centerX - btnWHalf - index * (btnW + space) - margin, centerY +adjushY - btnWHalf) : CGPointMake(centerX +btnWHalf, centerY +adjushY - btnWHalf);
    };
 case animationTypeShowLeft:{
            self.pointLeft(YES,index);
            break;
        }
        case animationTypeShowRight:{
            self.pointRight(YES,index);
            break;
        }
        case animationTypeShowLevel:
            if (index <= LeftCount) {
                self.pointLeft(YES,index);
            }else{
                self.pointRight(YES,index - LeftCount - 1);
            }
            break;
        default:
            break;

动画分明细

  CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAnimation.duration=.3;
        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        positionAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];
        positionAnimation.toValue = [NSValue valueWithCGPoint:endPoint];
        
        CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.additive = YES;
        scaleAnimation.values = @[@0,@(-1),@0];
        scaleAnimation.keyTimes =_currentBtn.isSelected ? @[@0,@0,@1] : @[@0,@1,@1];
        scaleAnimation.duration=.3;
        CAAnimationGroup *animationGG = [CAAnimationGroup animation];
        animationGG.duration = .3;
        animationGG.repeatCount = 1;
        animationGG.animations = @[positionAnimation, scaleAnimation];
        animationGG.fillMode = kCAFillModeBoth;
        animationGG.removedOnCompletion = YES;
        animationGG.beginTime =  _currentTime + (0.3/(float)_btnMarr.count * (float)i);
        [btn.layer addAnimation:animationGG forKey:nil];
        btn.layer.position = endPoint;

这里强调一点position对应的是视图在父视图中锚点所处的位置而 我们的锚点默认的是{0.5,0.5}

github源代码

有问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

上一篇下一篇

猜你喜欢

热点阅读