国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果

2020-07-26 02:47:52
字體:
來源:轉載
供稿:網(wǎng)友

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

效果圖

脈沖數(shù)量、速度、半徑、透明度、漸變顏色、方向等都可以設置。可以用于地圖標注(Annotation)、按鈕長按動畫效果(例如錄音按鈕)等。

代碼已上傳 GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo

實現(xiàn)原理

實現(xiàn)方法參考:https://github.com/shu223/Pulsator

但是覺得那些代碼不夠簡潔,所以自己寫了一個,還加了些功能。

自定義 PulsatorLayer,繼承自 CAReplicatorLayer。CAReplicatorLayer 可以復制子圖層(Sublayer),被復制出來的子圖層可以改變位置、顏色等屬性。每一個脈沖(一個漸變的圓形)就是一個被復制出來的子圖層。

顯示脈沖的圖層就是子圖層,把它作為 pulseLayer 屬性

private var pulseLayer: CALayer!

脈沖子圖層一開始不顯示,因此初始化時為全透明;通過設置圓角,使 pulseLayer 為圓形

pulseLayer = CALayer()pulseLayer.opacity = 0pulseLayer.backgroundColor = outColorpulseLayer.contentsScale = UIScreen.main.scalepulseLayer.bounds.size = CGSize(width: maxRadius * 2, height: maxRadius * 2)pulseLayer.cornerRadius = maxRadiusaddSublayer(pulseLayer)

設置 CAReplicatorLayer 的一些屬性

// The number of copies to create, including the source layersinstanceCount// Specifies the delay, in seconds, between replicated copiesinstanceDelay

設置復制子圖層的數(shù)量、創(chuàng)建兩個子圖層之間的時間間隔。

CAReplicatorLayer 遵循 CAMediaTiming 協(xié)議,設置協(xié)議屬性

// Determines the number of times the animation will repeatrepeatCount = MAXFLOAT

把動畫重復次數(shù)設置為很大的數(shù),讓動畫一直重復。

動畫效果由 3 個 CABasicAnimation 組成,分別改變脈沖的大小、透明度、背景色顏色

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale.xy")scaleAnimation.duration = animationDurationlet opacityAnimation = CABasicAnimation(keyPath: "opacity")opacityAnimation.duration = animationDurationlet colorAnimation = CABasicAnimation(keyPath: "backgroundColor")colorAnimation.duration = animationDurationswitch 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 + animationIntervalanimationGroup.animations = [scaleAnimation, opacityAnimation, colorAnimation]animationGroup.repeatCount = repeatCountpulseLayer.add(animationGroup, forKey: kPulseAnimationKey)

以上代碼判斷了脈沖的方向(由內(nèi)向外、由外向內(nèi)),兩種方向的動畫屬性起止取值相反。把這 3 個 CABasicAnimation 加入 CAAnimationGroup 中一起執(zhí)行。

以上就是實現(xiàn)原理與最核心的代碼,具體見 GitHub:https://github.com/Silence-GitHub/CoreAnimationDemo

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 闵行区| 东丽区| 庄浪县| 孙吴县| 志丹县| 九寨沟县| 卓尼县| 岱山县| 崇信县| 高陵县| 古交市| 北碚区| 城步| 北安市| 岱山县| 临朐县| 南部县| 汨罗市| 蓬莱市| 磐安县| 盐池县| 竹溪县| 桃园市| 东台市| 沈丘县| 施秉县| 晋城| 杭州市| 抚州市| 博爱县| 长垣县| 锦州市| 长沙县| 叙永县| 昭平县| 武义县| 邳州市| 正安县| 龙陵县| 玛纳斯县| 昌乐县|