我想知道是否有可能一个闪烁的动画应用于一个UIButton。 我已经搜查,但只找到了连续变化我的UIButton大小的脉冲动画的代码。 我不是在想着某种闪烁的动画,提醒他要按下按钮的用户。 这个问题的唯一办法,我可以通过改变阿尔法不断利用认为的:
[self setAlpha:0.5];
...但它不会像闪烁的按钮可见。
我想知道是否有可能一个闪烁的动画应用于一个UIButton。 我已经搜查,但只找到了连续变化我的UIButton大小的脉冲动画的代码。 我不是在想着某种闪烁的动画,提醒他要按下按钮的用户。 这个问题的唯一办法,我可以通过改变阿尔法不断利用认为的:
[self setAlpha:0.5];
...但它不会像闪烁的按钮可见。
也许不是最好的方式,并没有真正让你停止闪烁......但是这是简单,有效,并且不会妨碍用户互动:
- (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];
}];
}
阅读所有的答案到目前为止,我发现了以下解决方案。 它重复了很多次,并做这项工作对我来说。
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;
}];
试试这个:
当你想闪烁/闪光按钮调用此方法
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];
也许你可以有两个不同的.png文件[按钮]这个周期在一个回环往复,同样的动作分配给他们,只要满足特定条件有这个开球。 我写的代码了,但它很可能是充满了错误。 您是否尝试过类似的东西?
我创建我自己的控制,子类UIControl,因为苹果不建议使用的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"];
}