在這篇文章里我們來(lái)實(shí)現(xiàn)一個(gè) Android 里官方提供的一個(gè)下拉刷新控件 SwipeRefreshLayout.
整體效果如下:
要實(shí)現(xiàn)這樣的一個(gè)效果, 有以下幾個(gè)問(wèn)題需要解決.
動(dòng)畫(huà)部分
- 三角和圓弧線(xiàn)是怎么繪制的? 怎么控制它的運(yùn)動(dòng)軌跡?
- 這種三段式的圓弧線(xiàn), 它的淡入淡出的動(dòng)畫(huà)效果是怎么實(shí)現(xiàn)的.
刷新部分
- 不通過(guò)ScrollView 的滾動(dòng)偏移值, 如何在 ScrollView 的頂部觸發(fā)刷新操作?
動(dòng)畫(huà)部分
整個(gè)動(dòng)畫(huà)部分我們都是通過(guò) CAShapeLayer
實(shí)現(xiàn)的.
CALayer
的子類(lèi) CAShapeLayer
是一個(gè)通過(guò)矢量圖形而不是 bitmap
來(lái)繪制的圖層類(lèi). 繪制路徑即可以用 CAShapeLayer
, 也可以用 Core Graphics
直接向原始的 CALayer
的內(nèi)容中繪制一個(gè)路徑. 相比之下, 使用 CAShapeLayer
有以下一些優(yōu)點(diǎn):
- 渲染快速.
CAShapeLayer
使用了硬件加速, 繪制同一圖形會(huì)比用Core Graphics
快很多. - 高效使用內(nèi)存. 一個(gè)
CAShapeLayer
不需要像普通CALayer
一樣創(chuàng)建一個(gè)寄宿圖形, 所以無(wú)論有多大, 都不會(huì)占用太多的內(nèi)存. - 不會(huì)被圖層邊界剪裁掉. 一個(gè)
CAShapeLayer
可以在邊界之外繪制. 你的圖層路徑不會(huì)像在使用Core Graphics
的普通CALayer
一樣被剪裁掉. - 不會(huì)出現(xiàn)像素化. 當(dāng)你把
CAShapeLayer
放大, 或是用3D透視變換將其離相機(jī)更近時(shí), 它不像一個(gè)有寄宿圖的普通圖層一樣變得像素化.
下面畫(huà)一個(gè)火柴人演示 CAShapeLayer
的使用:
代碼實(shí)現(xiàn)
//創(chuàng)建路徑
let path = UIBezierPath()
// 定義起點(diǎn)
path.move(to: CGPoint(x: 175, y: 100))
// 繪制圓
path.addArc(withCenter: CGPoint(x: 150, y: 100), radius: 25, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
// 定義起點(diǎn)
path.move(to: CGPoint(x: 150, y: 125))
// 繪制直線(xiàn)
path.addLine(to: CGPoint(x: 150, y: 175))
path.addLine(to: CGPoint(x: 125, y: 225))
path.move(to: CGPoint(x: 150, y: 175))
path.addLine(to: CGPoint(x: 175, y: 225))
path.move(to: CGPoint(x: 100, y: 150))
path.addLine(to: CGPoint(x: 200, y: 150))
//創(chuàng)建shape layer
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.red.cgColor // 路徑顏色
shapeLayer.fillColor = UIColor.clear.cgColor // 封閉區(qū)間填充顏色
shapeLayer.lineWidth = 5 // 線(xiàn)條寬度
shapeLayer.lineJoin = .round // 線(xiàn)條終點(diǎn)樣式
shapeLayer.lineCap = .round // 線(xiàn)條拐點(diǎn)樣式
shapeLayer.path = path.cgPath
//添加layer
view.layer.addSublayer(shapeLayer)
1. 如何通過(guò) shape layer 動(dòng)態(tài)畫(huà)圓?
let pathLayer = CAShapeLayer()
pathLayer.strokeStart = 0
pathLayer.strokeEnd = 10
pathLayer.fillColor = nil
pathLayer.lineWidth = 2.5
// 繪制圓形
let path = UIBezierPath(arcCenter: CGPoint(x: 20, y: 20), radius: 9, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
pathLayer.path = path.cgPath
pathLayer.lineCap = .square
view.layer.addSublayer(pathLayer)
通過(guò)改變 strokeStart / strokeEnd 可以實(shí)現(xiàn)動(dòng)態(tài)畫(huà)圓.
2. 如何繪制一個(gè)三角型
還是通過(guò)貝塞爾線(xiàn)來(lái)繪制路徑
let arrowLayer = CAShapeLayer()
arrowLayer.lineWidth = 10
arrowLayer.strokeColor = UIColor.blue.cgColor
arrowLayer.transform = CATransform3DMakeTranslation(100, 100, 0)
// 目標(biāo)點(diǎn)
let points = [CGPoint(x: 0, y: 1),
CGPoint(x: 1, y: 0),
CGPoint(x: 0, y: -1)]
// 旋轉(zhuǎn)90度
let transform: CGAffineTransform = CGAffineTransform(rotationAngle: .pi / 2)
let cgPath = CGMutablePath()
cgPath.addLines(between: points, transform: transform)
cgPath.closeSubpath()![image02.png](https://upload-images.jianshu.io/upload_images/1208639-3dca87508db1fa8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
arrowLayer.path = cgPath
view.layer.addSublayer(arrowLayer)
效果如下
通過(guò)旋轉(zhuǎn)父視圖, 結(jié)合三角, 圓的路徑, 就很容易實(shí)現(xiàn)下面這種效果.
3. 這種三段式的動(dòng)畫(huà)怎么實(shí)現(xiàn)的.
主要是利用基礎(chǔ)動(dòng)畫(huà)的組合動(dòng)畫(huà)實(shí)現(xiàn)的.
let beginHeadAnimation = CABasicAnimation()
beginHeadAnimation.keyPath = "strokeStart"
beginHeadAnimation.duration = 0.5
beginHeadAnimation.fromValue = 0.25
beginHeadAnimation.toValue = 1.0
beginHeadAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
let beginTailAnimation = CABasicAnimation()
beginTailAnimation.keyPath = "strokeEnd"
beginTailAnimation.duration = 0.5
beginTailAnimation.fromValue = 1.0
beginTailAnimation.toValue = 1.0
beginTailAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
let endHeadAnimation = CABasicAnimation()
endHeadAnimation.keyPath = "strokeStart"
endHeadAnimation.beginTime = 0.5
endHeadAnimation.duration = 1.0
endHeadAnimation.fromValue = 0.0
endHeadAnimation.toValue = 0.25
endHeadAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
let endTailAnimation = CABasicAnimation()
endTailAnimation.keyPath = "strokeEnd"
endTailAnimation.beginTime = 0.5
endTailAnimation.duration = 1.0
endTailAnimation.fromValue = 0.0
endTailAnimation.toValue = 1.0
endTailAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
let animations = CAAnimationGroup()
animations.duration = 1.5
animations.isRemovedOnCompletion = false
animations.animations = [beginHeadAnimation, beginTailAnimation, endHeadAnimation, endTailAnimation]
animations.repeatCount = .infinity
pathLayer.add(animations, forKey: "stroke_animation")
let timer = Timer(timeInterval: 0.5, target: self, selector: #selector(changeColor), userInfo: nil, repeats: false)
RunLoop.current.add(timer, forMode: .common)
@objc func changeColor() {
// 變化顏色
colorIndex += 1
if colorIndex > colors.count - 1 {
colorIndex = 0
}
CATransaction.begin()
CATransaction.setDisableActions(true)
pathLayer.strokeColor = colors[colorIndex]
CATransaction.commit()
let timer = Timer(timeInterval: 1.5, target: self, selector: #selector(changeColor), userInfo: nil, repeats: false)
RunLoop.current.add(timer, forMode: .common)
}
效果如下
刷新部分
一般的刷新框架都是采用的 KVO 或者代理獲取 ScrollView 的縱向偏移值來(lái)顯示動(dòng)畫(huà)的. 但是 SwipeRefreshLayout 刷新框架并不是這樣的, 我猜測(cè)應(yīng)該是使用手勢(shì), 通過(guò) pan 手勢(shì)存儲(chǔ)滑動(dòng)的縱向值來(lái)達(dá)到 ScrollView 類(lèi)似的效果. 這種也是可行的.