swift - button中间扩散动画

看见一个漂亮的按钮动画现将其实现从中间扩散的动画拿去不谢!看起来还是挺屌的无非是使用CAShapreLayer和 CABasicAnimation进行封装 (个人还是希望能有相关从业者看到这篇文章,以前我写的东西都不会这样精细地去打磨下?因为我不会告诉你我以前写都是写cashapelayer),关于这两个不过多的说它们具体的细节
本篇只需要知道如何利用CAShapreLayer和CGPath来做一个算是比较精美的颜色扩散动画

以下会实现一个UIview的扩展,显示的效果类似推特的开启界面效果

//
//  ExpandAnimateButton.swift
//  SlideAuthorizon
//
//  Created by lotawei on 2018/1/31.
//  Copyright © 2018年 lotawei. All rights reserved.
//

import Foundation
import UIKit
import  QuartzCore
let ButtonPadding:CGFloat = 100

//主要是在动画完成时可以进行一些操作
class AnimationDelegate: NSObject, CAAnimationDelegate {
    
    fileprivate let completion: () -> Void
    
    init(completion: @escaping () -> Void) {
        self.completion = completion
    }
    
    func animationDidStop(_: CAAnimation, finished: Bool) {
        completion()
    }
}
//封装任意的视图可以进行变化的 
extension UIView {
    
    func animateCircular(withDuration duration: TimeInterval, center: CGPoint, revert: Bool = false, animations: () -> Void, completion: ((Bool) -> Void)? = nil) {
        let snapshot = snapshotView(afterScreenUpdates: false)!
        snapshot.frame = bounds
        self.addSubview(snapshot)
        
        let center = convert(center, to: snapshot)
        let radius: CGFloat = {
            let x = max(center.x, frame.width - center.x)
            let y = max(center.y, frame.height - center.y)
            return sqrt(x * x + y * y)
        }()
        var animation : CircularRevealAnimator
        if !revert {
            animation = CircularRevealAnimator(layer: snapshot.layer, center: center, startRadius: 0, endRadius: radius, invert: true)
        } else {
            animation = CircularRevealAnimator(layer: snapshot.layer, center: center, startRadius: radius, endRadius: 0, invert: false)
        }
        animation.duration = duration
        animation.completion = {
            snapshot.removeFromSuperview()
            completion?(true)
        }
        animation.start()
        animations()
    }
}
private func SquareAroundCircle(_ center: CGPoint, radius: CGFloat) -> CGRect {
    assert(0 <= radius, "请修改你的半径,它不能为0")
    return CGRect(origin: center, size: CGSize.zero).insetBy(dx: -radius, dy: -radius)
}
class CircularRevealAnimator {
    
    var completion: (() -> Void)?
    
    fileprivate let layer: CALayer
    fileprivate let mask: CAShapeLayer
    fileprivate let animation: CABasicAnimation
    
    var duration: CFTimeInterval {
        get { return animation.duration }
        set(value) { animation.duration = value }
    }
    
    var timingFunction: CAMediaTimingFunction! {
        get { return animation.timingFunction }
        set(value) { animation.timingFunction = value }
    }
    
    init(layer: CALayer, center: CGPoint, startRadius: CGFloat, endRadius: CGFloat, invert: Bool = false) {
        let startCirclePath = CGPath(ellipseIn: SquareAroundCircle(center, radius: startRadius), transform: nil)
        let endCirclePath = CGPath(ellipseIn: SquareAroundCircle(center, radius: endRadius), transform: nil)
        
        var startPath = startCirclePath, endPath = endCirclePath
        if invert {
            var path = CGMutablePath()
            path.addRect(layer.bounds)
            path.addPath(startCirclePath)
            startPath = path
            path = CGMutablePath()
            path.addRect(layer.bounds)
            path.addPath(endCirclePath)
            endPath = path
        }
        
        self.layer = layer
        
        mask = CAShapeLayer()
        mask.path = endPath
        mask.fillRule = kCAFillRuleEvenOdd
        
        animation = CABasicAnimation(keyPath: "path")
        animation.fromValue = startPath
        animation.toValue = endPath
        animation.delegate = AnimationDelegate {
            layer.mask = nil
            self.completion?()
            self.animation.delegate = nil
        }
    }
    
    func start() {
        layer.mask = mask
        mask.frame = layer.bounds
        mask.add(animation, forKey: "reveal")
    }
}

@IBDesignable class ExpandAnimateButton: UIButton {
    
    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + ButtonPadding, height: size.height)
    }
    
    func animateTouchUpInside(completion: @escaping () -> Void) {
        isUserInteractionEnabled = false
        layer.masksToBounds = true
        
        let fillLayer = CALayer()
        fillLayer.backgroundColor = layer.borderColor
        fillLayer.frame = layer.bounds
        layer.insertSublayer(fillLayer, at: 0)
        
        let center = CGPoint(x: fillLayer.bounds.midX, y: fillLayer.bounds.midY)
        let radius: CGFloat = max(frame.width / 2 , frame.height / 2)
        
        let circularAnimation = CircularRevealAnimator(layer: fillLayer, center: center, startRadius: 0, endRadius: radius)
        circularAnimation.duration = 0.2
        circularAnimation.timingFunction =  CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseIn)
        circularAnimation.completion = {
            fillLayer.opacity = 0
            let opacityAnimation = CABasicAnimation(keyPath: "opacity")
            opacityAnimation.fromValue = 1
            opacityAnimation.toValue = 0
            opacityAnimation.duration = 0.2
            opacityAnimation.delegate = AnimationDelegate {
                fillLayer.removeFromSuperlayer()
                self.isUserInteractionEnabled = true
                completion()
            }
            fillLayer.add(opacityAnimation, forKey: "opacity")
        }
        circularAnimation.start()
    }
}

//使用方式

sender.animateTouchUpInside {
            print("fuck")
            self.circleview.animateCircular(withDuration: 0.2, center: self.circleview.center, animations: {
                 self.islight = !self.islight
            })
            
        }

代码地址:https://github.com/lwiosbystep/LTAuthorizonView
包含验证、只需要使用按钮和扩散动画的话使用ExpandAnimateButton即可

你可能感兴趣的:(swift - button中间扩散动画)