2015-04-01 3 views
2

IAM пытается создать пульсирующую анимацию для UIButton Это мои коды до сих пор, но до сих пор я не в состоянии сделать точную анимацию, показанную в этой ссылкеКак я могу создать пульсирующую анимацию для UIButton iOS?

CSS pulsating button

Мой код

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]; 

ответ

7

Попробуйте это.

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]; 
+0

ok добавив это в мой проект –

+0

круто это работает –

+0

Спасибо. Его работа прекрасна. – Raja

Смежные вопросы