我想在一个UIButton上有一些脉冲动画(无限循环"放大-缩小"),这样它就能立即引起用户的注意.

我看到了这个链接How to create a pulse effect using -webkit-animation - outward rings,但我想知道是否有办法只使用本机框架来实现这一点?

推荐答案

CABasicAnimation *theAnimation;

theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=1.0;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.0];
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"]; //myButton.layer instead of

Swift

let pulseAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
pulseAnimation.duration = 1
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
view.layer.add(pulseAnimation, forKey: "animateOpacity")

See the article "Animating Layer Content"

Ios相关问答推荐

如何将模糊输入/模糊输出添加到SwiftUI中的非对称过渡?

拖动手势导致 sim 崩溃 Swift UI

如何在Combine发布者之间强制执行最小延迟

比较 `某个协议` 实例时出现编译错误

创建方案时运行 pod install 时出错

长按 SwiftUI 更改项目的顺序

核心媒体基础知识,了解 CMSampleBuffer、CMBlockBuffer 和 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer

SwiftUI Disclosure Group 文本对齐

SwiftUI 中的偏移量和按钮有问题吗?

一个 SwiftUI 视图中的多个垂直滚动视图

来自 c++ 的runtime_error未在 iOS 中捕获

如何在不支持并发的自动关闭中修复'async'调用?

我可以使用同一个 Firebase 应用将多个 Flutter 应用添加到一个 Firebase 数据库吗?

通过MatchedGeometryEffect向上滑动视图

如何在保持纵横比的同时zoom UIView 的内容以适应目标矩形?

如何在 Swift 上的 sendSynchronousRequest 中判断 Response.statusCode

SwiftUI NavigationLink 立即加载目标视图,无需单击

Swift 错误:在其自己的初始值中使用的变量

滑动列表项以获取更多选项(Flutter)

关闭所有打开的视图控制器的单个函数