iOS CAReplicatorLayer實(shí)現(xiàn)脈沖動(dòng)畫效果
iOS CAReplicatorLayer 實(shí)現(xiàn)脈沖動(dòng)畫效果,供大家參考,具體內(nèi)容如下
效果圖

脈沖數(shù)量、速度、半徑、透明度、漸變顏色、方向等都可以設(shè)置??梢杂糜诘貓D標(biāo)注(Annotation)、按鈕長按動(dòng)畫效果(例如錄音按鈕)等。
代碼已上傳 GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo
實(shí)現(xiàn)原理
實(shí)現(xiàn)方法參考:https://github.com/shu223/Pulsator
但是覺得那些代碼不夠簡潔,所以自己寫了一個(gè),還加了些功能。
自定義 PulsatorLayer,繼承自 CAReplicatorLayer。CAReplicatorLayer 可以復(fù)制子圖層(Sublayer),被復(fù)制出來的子圖層可以改變位置、顏色等屬性。每一個(gè)脈沖(一個(gè)漸變的圓形)就是一個(gè)被復(fù)制出來的子圖層。
顯示脈沖的圖層就是子圖層,把它作為 pulseLayer 屬性
private var pulseLayer: CALayer!
脈沖子圖層一開始不顯示,因此初始化時(shí)為全透明;通過設(shè)置圓角,使 pulseLayer 為圓形
pulseLayer = CALayer() pulseLayer.opacity = 0 pulseLayer.backgroundColor = outColor pulseLayer.contentsScale = UIScreen.main.scale pulseLayer.bounds.size = CGSize(width: maxRadius * 2, height: maxRadius * 2) pulseLayer.cornerRadius = maxRadius addSublayer(pulseLayer)
設(shè)置 CAReplicatorLayer 的一些屬性
// The number of copies to create, including the source layers instanceCount // Specifies the delay, in seconds, between replicated copies instanceDelay
設(shè)置復(fù)制子圖層的數(shù)量、創(chuàng)建兩個(gè)子圖層之間的時(shí)間間隔。
CAReplicatorLayer 遵循 CAMediaTiming 協(xié)議,設(shè)置協(xié)議屬性
// Determines the number of times the animation will repeat repeatCount = MAXFLOAT
把動(dòng)畫重復(fù)次數(shù)設(shè)置為很大的數(shù),讓動(dòng)畫一直重復(fù)。
動(dòng)畫效果由 3 個(gè) CABasicAnimation 組成,分別改變脈沖的大小、透明度、背景色顏色
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
scaleAnimation.duration = animationDuration
let opacityAnimation = CABasicAnimation(keyPath: "opacity")
opacityAnimation.duration = animationDuration
let colorAnimation = CABasicAnimation(keyPath: "backgroundColor")
colorAnimation.duration = animationDuration
switch pulseOrientation {
case .out:
scaleAnimation.fromValue = minRadius / maxRadius
scaleAnimation.toValue = 1
opacityAnimation.fromValue = maxAlpha
opacityAnimation.toValue = minAlpha
colorAnimation.fromValue = inColor
colorAnimation.toValue = outColor
case .in:
scaleAnimation.fromValue = 1
scaleAnimation.toValue = minRadius / maxRadius
opacityAnimation.fromValue = minAlpha
opacityAnimation.toValue = maxAlpha
colorAnimation.fromValue = outColor
colorAnimation.toValue = inColor
}
let animationGroup = CAAnimationGroup()
animationGroup.duration = animationDuration + animationInterval
animationGroup.animations = [scaleAnimation, opacityAnimation, colorAnimation]
animationGroup.repeatCount = repeatCount
pulseLayer.add(animationGroup, forKey: kPulseAnimationKey)
以上代碼判斷了脈沖的方向(由內(nèi)向外、由外向內(nèi)),兩種方向的動(dòng)畫屬性起止取值相反。把這 3 個(gè) CABasicAnimation 加入 CAAnimationGroup 中一起執(zhí)行。
以上就是實(shí)現(xiàn)原理與最核心的代碼,具體見 GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- IOS等待時(shí)動(dòng)畫效果的實(shí)現(xiàn)
- IOS開發(fā)代碼分享之獲取啟動(dòng)畫面圖片的string
- IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動(dòng)畫詳解
- IOS繪制動(dòng)畫顏色漸變折線條
- iOS實(shí)現(xiàn)滾動(dòng)字幕的動(dòng)畫特效
- 詳解iOS開發(fā)中的轉(zhuǎn)場動(dòng)畫和組動(dòng)畫以及UIView封裝動(dòng)畫
- 仿IOS效果 帶彈簧動(dòng)畫的ListView
- IOS實(shí)現(xiàn)視頻動(dòng)畫效果的啟動(dòng)圖
- IOS輕松幾步實(shí)現(xiàn)自定義轉(zhuǎn)場動(dòng)畫
- IOS 圓球沿著橢圓軌跡做動(dòng)畫
相關(guān)文章
iOS開發(fā)實(shí)現(xiàn)簡單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)簡單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
詳解iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲(chǔ)方式
這篇文章主要介紹了iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲(chǔ)方式,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS 監(jiān)聽回調(diào)機(jī)制KVO實(shí)例
下面小編就為大家分享一篇iOS 監(jiān)聽回調(diào)機(jī)制KVO實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供兩種實(shí)現(xiàn)方法分別是ViewControllViewController方法和 ViewControllViewController 與 xib方法,需要的朋友可以參考下2017-10-10
iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果
這篇文章主要介紹了iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果,整體思路是我們將tableView 的contentinset設(shè)置為上面一個(gè)屏幕的高度,下面一個(gè)屏幕的高度,左右為0,這樣保證我們滾動(dòng)過去的時(shí)候2024-06-06
都是準(zhǔn)備好的內(nèi)容,需要的朋友可以參考下

