码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-05-11 22:08:30      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

个人技术博客站欢迎您

技术分享
创建一个继承于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地址 点赞喔
个人技术博客站欢迎您

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

标签:

原文地址:http://blog.csdn.net/sinat_30162391/article/details/51377938

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!