Swift版本仿网易云音乐播放音乐动画效果

前端之家收集整理的这篇文章主要介绍了Swift版本仿网易云音乐播放音乐动画效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

个人技术博客站欢迎您


创建一个继承于UIView的视图我们叫他XTActivityView

/// 立方柱的个数
    var numberOfRect = 0;
        /// 立方柱的颜色
    var rectBackgroundColor: UIColor?
        /// 立方柱初始化大小
    var defaultSize: CGSize?
        /// 立方柱之间的间距
    var space: CGFloat = 0.0

进行初始化

override init(frame: CGRect) {
        super.init(frame: frame)
        self.createDefaultAttribute(frame)
    }
func createDefaultAttribute(frame: CGRect) -> Void {
        numberOfRect = 6;
        self.rectBackgroundColor = UIColor.blackColor()
        space = 1;
        defaultSize = frame.size
    }

提供开始和结束接口(即创建和移除)

func startAnimation() {
        self.addRect()
    }
func stopAnimation() {
        self.removeRect()
    }

添加矩形

func addRect() -> Void {
        self.removeRect()
        self.hidden = false
        for  i in 0...numberOfRect {
        let x = (CGFloat)(i) * (5 + space)
        let rView = UIView.init(frame: CGRectMake(x,0,5,defaultSize!.height))
            rView.backgroundColor = rectBackgroundColor
            rView.layer.addAnimation(self.addAnimateWithDelay((Double)(i) * 0.2),forKey: "TBRotate")
        self.addSubview(rView)
        }
    }

动画效果

func addAnimateWithDelay(delay: Double) -> CAAnimation {
        let animation = CABasicAnimation.init(keyPath: "transform.rotation.x")
        animation.repeatCount = MAXFLOAT;
        animation.removedOnCompletion = true;
        animation.autoreverses = false;
        animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
        animation.fromValue = NSNumber.init(float: 0)
        animation.toValue = NSNumber.init(double: M_PI)
        animation.duration = (Double)(numberOfRect) * 0.2;
        animation.beginTime = CACurrentMediaTime() + delay;
        return animation;
    }

移除矩形

func removeRect() -> Void {
        if self.subviews.count > 0 {
            self.removeFromSuperview()
        }
        self.hidden = true
    }

在控制器中调用

self.view.backgroundColor = UIColor.whiteColor()
let xt = XTActivityView.init(frame: CGRectMake(0,10,80,15))
xt.startAnimation()
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: xt)
xt.performSelector(Selector("stopAnimation"),withObject: nil,afterDelay: 5)

各位大大我是demo地址 点赞喔
个人技术博客站欢迎您

原文链接:https://www.f2er.com/swift/323827.html

猜你在找的Swift相关文章