UIButton闪烁动画

时间:2013-03-12 17:47:00

标签: ios animation uikit

我想知道是否可以将闪烁动画应用于UIButton。我已经搜索但只找到了脉冲动画的代码,它会不断改变我的UIButton的大小。我改为考虑某种闪烁动画来提醒用户他必须按下按钮。我能想到的唯一解决问题的方法是不断改变alpha:

[self setAlpha:0.5]; 

...但它不会像闪烁的按钮那样可见。

5 个答案:

答案 0 :(得分:16)

也许不是最好的方式,并且真的不允许你停止闪烁...但这很简单,有效,并且不会妨碍用户互动:

- (void)viewDidLoad
{
    [self flashOn:myButton];
}

- (void)flashOff:(UIView *)v
{
    [UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
        v.alpha = .01;  //don't animate alpha to 0, otherwise you won't be able to interact with it
    } completion:^(BOOL finished) {
        [self flashOn:v];
    }];
}

- (void)flashOn:(UIView *)v
{
    [UIView animateWithDuration:.05 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
        v.alpha = 1;
    } completion:^(BOOL finished) {
        [self flashOff:v];
    }];
}

答案 1 :(得分:5)

到目前为止阅读所有答案我找到了以下解决方案。它重复了很多次并为我完成了工作。

CGFloat oldAlpha = v.alpha;
CGFloat minAlpha = 0.2;
enum UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut;

[UIView animateWithDuration:.3 delay:0.3 options:options animations:^{
    [UIView setAnimationRepeatCount:4];
    v.alpha = minAlpha;
}
completion:^(BOOL finished) {
    v.alpha = oldAlpha;
}];

答案 2 :(得分:4)

试试这个:

当您想要闪烁/闪烁按钮

时调用此方法
blinkTimer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(toggleButtonImage:) userInfo:nil repeats:YES];

并编写此方法

- (void)toggleButtonImage:(NSTimer*)timer
{

    if(toggle)
    {
        [blinkBtn setImage:[UIImage imageNamed:@"ButtonImage.png"] forState: UIControlStateNormal];
    }
    else
    {
        [blinkBtn setImage:[UIImage imageNamed:@"ButtonImage1.png"] forState: UIControlStateNormal];
    }
    toggle = !toggle;

}

在.h写这个

NSTimer *blinkTimer;
BOOL toggle;

并使您想要停止闪烁/闪烁的计时器无效

[blinkTimer invalidate];

答案 3 :(得分:1)

也许你可以有两个不同的.png文件[按钮]在循环中来回循环,为它们分配相同的动作,并在满足某个条件时启动。我会把代码写出来,但它可能会充满错误。你尝试过类似的东西吗?

答案 4 :(得分:1)

我通过创建自己的控件,子类化UIControl来实现这一点,因为Apple并不建议使用UIButton的视图层次结构。我添加了一个背景图像视图,代表按钮的标准背景图像,以及一个发光的" imageView在背景上方表示点亮状态,并切换其不透明度以使其成为脉冲。

我还要切换图层的阴影不透明度,使其发光。

初始化代码:

- (void)TS_commonButtonInit
{
    UIImage *shoutoutBackground            = [UIImage imageNamed:@"root-navigation-bar-share-button"];
    UIImage *shoutoutHighlightedBackground = [UIImage imageNamed:@"root-navigation-bar-share-button-highlighted"];
    UIImage *shoutoutPulseImage            = [UIImage imageNamed:@"root-navigation-bar-share-button-glowing"];

    shoutoutBackground            = [shoutoutBackground            stretchableImageWithLeftCapWidth:7 topCapHeight:0];
    shoutoutHighlightedBackground = [shoutoutHighlightedBackground stretchableImageWithLeftCapWidth:7 topCapHeight:0];
    shoutoutPulseImage            = [shoutoutPulseImage            stretchableImageWithLeftCapWidth:7 topCapHeight:0];

    [[self backgroundView] setImage:shoutoutBackground];
    [[self backgroundView] setHighlightedImage:shoutoutHighlightedBackground];

    [self setGlowingImage:shoutoutPulseImage];

    [self setExclusiveTouch:YES];

    [self addSubview:[self backgroundView]];
    [self addSubview:[self glowingImageView]];

    [[self layer] setShadowColor:[[UIColor colorWithHexString:@"ffc521" alpha:1] CGColor]];
    [[self layer] setShadowOpacity:0];
    [[self layer] setShadowRadius:5];
    [[self layer] setShadowOffset:CGSizeMake(0, 0)];
    [[self layer] setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:6] CGPath]];
}

Pulsing Code:

- (void)pulse:(NSInteger)numberOfTimes
{
    CGFloat pulseLength = .8;

    [[self glowingImageView] setAlpha:0];

    CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [pulseAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [pulseAnimation setDuration:pulseLength];
    [pulseAnimation setRepeatCount:numberOfTimes];
    [pulseAnimation setAutoreverses:YES];
    [pulseAnimation setFromValue:@(0)];
    [pulseAnimation setToValue:@(1)];
    [pulseAnimation setRemovedOnCompletion:YES];

    [[self layer] setShadowOpacity:0];

    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    [shadowAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [shadowAnimation setDuration:pulseLength];
    [shadowAnimation setRepeatCount:numberOfTimes];
    [shadowAnimation setAutoreverses:YES];
    [shadowAnimation setFromValue:@(0)];
    [shadowAnimation setToValue:@(1)];
    [shadowAnimation setRemovedOnCompletion:YES];

    [[[self glowingImageView] layer] addAnimation:pulseAnimation forKey:@"opacity"];
    [[self layer] addAnimation:shadowAnimation forKey:@"shadowOpacity"];
}