UIbutton flashing动画
我想知道是否可以将闪烁的动画应用到UIbutton。我在互联网上搜索,但只发现脉冲动画的代码,不断改变我的UIBUTTON的大小。我反而想着某种闪烁的动画来提醒用户他必须按下按钮。以我能想到的问题的唯一方法是通过改变阿尔法不断利用:UIbutton flashing动画
[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];
}];
}
您可以使用'UIViewAnimationOptionAutoreverse'动画选项简化代码。此外,如果您不想永远循环播放动画,请从动画块内部调用'setAnimationRepeatCount:'。 – 2013-03-12 18:10:23
通过使用你的代码和UIview到UIBarButtonItem我得到的错误:在UIBarButtonItem类型的对象上找不到属性alpha – Alessandro 2013-03-12 18:14:15
这是因为UIBarButtonItem不是UIView的子类。你说你有一个你想要刷新的UIButton,它确实是UIView的一个子类。 – MikeS 2013-03-12 18:22:10
也许你可以有两种不同的.png文件[按钮]这个周期在一个回环往复,同样的动作分配给他们,有这个揭开序幕只要满足特定条件。我会写出代码,但它可能会充满错误。你尝试过那样的事吗?
你的意思是swopping图像?我认为要做一些渐进的事情,亮度逐渐降低 – Alessandro 2013-03-12 17:57:36
哦,你的意思是像一种脉动式的外观?嗯。雷文德里克有一个有趣的教程,使应用程序的克隆“清除”。这是一个做清单应用程序。无论如何,他以UITableViewCell的编程方式实现了颜色淡化。我会检查一下这个项目,看看你能否像他那样使用相同的颜色原则。再次,这只是我的头顶。 – STANGMMX 2013-03-12 18:00:05
你可以给我的链接,谢谢 – Alessandro 2013-03-12 18:25:10
试试这个:
打电话时你想闪烁/闪光按钮
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];
我创造我自己的控制,子类UIControl,因为苹果这样做不建议使用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]];
}
脉动代码:
- (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"];
}
阅读所有的答案到目前为止,我发现了以下解决方案。它重复了很多次,为我完成这项工作。
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;
}];
感谢您的简明代码。如果您希望轻按按钮后立即淡入淡出,请在动画块之前设置v.alpha = minAlpha,并将块内的值设置为1.0或设置为oldAlpha。我觉得它看起来更像是用户触发了动画。 – neoscribe 2017-05-13 05:41:59
您是否尝试过使用两个按钮(或您的按钮上的一个虚拟图像)并在需要时隐藏/显示它? – 2013-03-12 17:54:22