UIbutton闪烁的animation

我想知道是否可以应用闪烁的animation到UIbutton。 我在互联网上search,但只发现了一个脉冲animation的代码,不断改变我的Uibutton的大小。 我反而想着某种闪烁的animation来提醒用户他必须按下button。 我能想到的问题的唯一方法是不断地使用alpha来改变alpha:

[self setAlpha:0.5]; 

…但它不会像闪烁的button一样可见。

也许不是最好的方法,并不真的让你停止闪烁…但这是简单的,工作,并不妨碍用户交互:

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

阅读所有答案到目前为止,我发现了以下解决scheme 它重复了很多次,为我做了这份工作。

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

试试这个:

当您想要闪烁/闪烁button时调用此方法

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

我通过创build我自己的控制,UIControl子类来做到这一点,因为苹果不build议拧与UIButton的视图层次结构。 我添加一个表示button的标准背景图像的背景图像视图,以及在背景之上的“发光”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"]; } 

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