首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为UIButton iOS创建一个脉动动画?

如何为UIButton iOS创建一个脉动动画?
EN

Stack Overflow用户
提问于 2015-04-01 07:31:26
回答 2查看 3.2K关注 0票数 3

我正试图为UIButton创建一个脉动动画--到目前为止,这些都是我的代码,但仍然无法完成这个链接中所显示的精确动画。

CSS脉动按钮

我的代码

代码语言:javascript
运行
复制
 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = [UIColor lightGrayColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [button addTarget:self
               action:@selector(method)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 160.0);
    CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulseAnimation.duration = .5;
    pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
    pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulseAnimation.autoreverses = YES;
    pulseAnimation.repeatCount = FLT_MAX;
    [button.layer addAnimation:pulseAnimation forKey:nil];
    button.layer.cornerRadius=button.frame.size.width / 2;
    ;
    button.layer.masksToBounds = YES;

    [self.view addSubview:button];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-01 07:52:18

尝尝这个。

代码语言:javascript
运行
复制
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 100, 100);
button.center = self.view.center;
[button setTitle:@"Button" forState:UIControlStateNormal];

UIView *c = [[UIView alloc] initWithFrame:button.bounds];
c.backgroundColor = [UIColor blueColor];
c.layer.cornerRadius = 50;
[button addSubview:c];
[button sendSubviewToBack:c];

UIView *f = [[UIView alloc] initWithFrame:button.bounds];
f.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.3];
f.layer.cornerRadius = 50;
[button addSubview:f];
[button sendSubviewToBack:f];

CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = .5;
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = MAXFLOAT;
[c.layer addAnimation:pulseAnimation forKey:@"a"];
[button.titleLabel.layer addAnimation:pulseAnimation forKey:@"a"];

CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
fade.toValue = @0;
CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulse.toValue = @2;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[fade,pulse];
group.duration = 1.0;
group.repeatCount = MAXFLOAT;
[f.layer addAnimation:group forKey:@"g"];

[self.view addSubview:button];
票数 8
EN

Stack Overflow用户

发布于 2015-04-01 08:01:57

一个很好的控制,可以根据需要调整。https://github.com/zackhsuan/ZKPulseView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29385195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档