iOS 浮动的button

最近在做一个小游戏,就希望把按钮能做的动态些,下面是一个简单的效果的button,

iOS 浮动的button_第1张图片
time1.gif

实现起来也非常简单,主要用到 CAKeyframeAnimation
主要代码段如下

-(void)setUI
{
    CAKeyframeAnimation *scaleX = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
    scaleX.values = @[[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.1],[NSNumber numberWithFloat:1.0]];
    scaleX.keyTimes = @[[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.0]];
    scaleX.repeatCount = MAXFLOAT;
    scaleX.autoreverses = YES;
    scaleX.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    scaleX.duration = 6;
    [self.layer addAnimation:scaleX forKey:@"scaleXAnimation"];
    CAKeyframeAnimation *scaleY = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
    scaleY.values = @[[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.1],[NSNumber numberWithFloat:1.0]];
    scaleY.keyTimes = @[[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.0]];
    scaleY.repeatCount = MAXFLOAT;
    scaleY.autoreverses = YES;
    scaleY.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    scaleY.duration = 6;
    [self.layer addAnimation:scaleX forKey:@"scaleYAnimation"];
}

我将他封装成一个CGBubbleBtn ,只要像正常使用button 那样使用即可,demo 地址如下
https://github.com/heysunnyboy/BubbleBtn

你可能感兴趣的:(iOS 浮动的button)