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

脈沖數(shù)量、速度、半徑、透明度、漸變顏色、方向等都可以設(shè)置。可以用于地圖標(biāo)注(Annotation)、按鈕長(zhǎng)按動(dòng)畫效果(例如錄音按鈕)等。
代碼已上傳 GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo
實(shí)現(xiàn)原理
實(shí)現(xiàn)方法參考:https://github.com/shu223/Pulsator
但是覺(jué)得那些代碼不夠簡(jiǎn)潔,所以自己寫了一個(gè),還加了些功能。
自定義 PulsatorLayer,繼承自 CAReplicatorLayer。CAReplicatorLayer 可以復(fù)制子圖層(Sublayer),被復(fù)制出來(lái)的子圖層可以改變位置、顏色等屬性。每一個(gè)脈沖(一個(gè)漸變的圓形)就是一個(gè)被復(fù)制出來(lái)的子圖層。
顯示脈沖的圖層就是子圖層,把它作為 pulseLayer 屬性
private var pulseLayer: CALayer!
脈沖子圖層一開始不顯示,因此初始化時(shí)為全透明;通過(guò)設(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)原理與最核心的代碼,具體見(jiàn) GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- IOS等待時(shí)動(dòng)畫效果的實(shí)現(xiàn)
- IOS開發(fā)代碼分享之獲取啟動(dòng)畫面圖片的string
- IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場(chǎng)動(dòng)畫詳解
- IOS繪制動(dòng)畫顏色漸變折線條
- iOS實(shí)現(xiàn)滾動(dòng)字幕的動(dòng)畫特效
- 詳解iOS開發(fā)中的轉(zhuǎn)場(chǎng)動(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)場(chǎng)動(dòng)畫
- IOS 圓球沿著橢圓軌跡做動(dòng)畫
相關(guān)文章
詳解iOS webview加載時(shí)序和緩存問(wèn)題總結(jié)
本篇文章主要介紹了iOS webview加載時(shí)序和緩存問(wèn)題總結(jié) ,這兩天學(xué)習(xí)了Vue.js 感覺(jué)組件這個(gè)地方知識(shí)點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。2017-09-09
iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController
UIActivityViewController類是一個(gè)標(biāo)準(zhǔn)的view controller,通個(gè)使用這個(gè)controller,你的應(yīng)用程序就可以提供各種服務(wù)。本文給大家介紹iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController,感興趣的朋友一起學(xué)習(xí)吧2015-11-11
IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場(chǎng)動(dòng)畫詳解
這篇文章主要介紹了IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場(chǎng)動(dòng)畫,CAAnimation的子類,用于做轉(zhuǎn)場(chǎng)動(dòng)畫,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭?dòng)畫效果,感興趣的小伙伴們可以參考一下2016-02-02
Objective-C中使用NSString類操作字符串的方法小結(jié)
這篇文章主要介紹了Objective-C中使用NSString類操作字符串的方法小結(jié),文中講到了字符串的分割和拼接等一些常見(jiàn)的用法,需要的朋友可以參考下2016-01-01
iOS Tabbar中間添加凸起可旋轉(zhuǎn)按鈕功能
最近的項(xiàng)目中有需求在tabbar中間添加凸起按鈕,并且點(diǎn)擊時(shí)按鈕要旋轉(zhuǎn),看了仿斗魚的凸起,點(diǎn)擊后是present出來(lái)View,這篇文章主要介紹了iOS Tabbar中間添加凸起可旋轉(zhuǎn)按鈕,需要的朋友可以參考下2017-12-12

