5

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

[self setAlpha:0.5]; 

...但它不会像闪烁的按钮那样明显。

4

5 回答 5

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];
    }];
}
于 2013-03-12T18:00:59.530 回答
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;
}];
于 2015-04-23T18:03:26.177 回答
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];
于 2013-03-12T19:00:13.410 回答
1

也许您可以有两个不同的 .png 文件 [按钮],它们在循环中来回循环,为它们分配相同的操作,并在满足特定条件时启动此操作。我会写出代码,但它可能会充满错误。你有没有尝试过这样的事情?

于 2013-03-12T17:54:26.913 回答
1

我通过创建自己的控件来实现这一点,将 UIControl 子类化,因为 Apple 不建议使用 UIButton 的视图层次结构。我添加了一个代表按钮的标准背景图像的背景 imageView,并在背景上方添加了一个“发光”的 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]];
}

脉冲代码:

- (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"];
}
于 2013-03-12T19:01:03.690 回答