水波纹动画和飘雪动画

雪花动画

水波纹动画和飘雪动画_第1张图片

    //雪花动画

    func animation1() {
        //粒子发射器
        let snowEmitter = CAEmitterLayer.init()
        //粒子发射的位置
        snowEmitter.emitterPosition = CGPointMake(self.view.frame.size.width/2, 30)
        //发射源的大小
        snowEmitter.emitterSize = CGSizeMake(self.view.bounds.size.width, 0.0)
        //发射模式
        snowEmitter.emitterMode = kCAEmitterLayerOutline
        //发射源的形状
        snowEmitter.emitterShape = kCAEmitterLayerLine
        //创建雪花粒子
        let snowflake = CAEmitterCell.init()
        //粒子的名称
        snowflake.name = "snow"
        //粒子参数的速度乘数因子。越大出现的越快
        snowflake.birthRate = 1.0
        //存活时间
        snowflake.lifetime = 120.0
        //粒子速度
        snowflake.velocity = -10
        // falling down slowly
        //粒子速度范围
        snowflake.velocityRange = 10
        //粒子y方向的加速度分量
        snowflake.yAcceleration = 2
        //周围发射角度
        snowflake.emissionRange = CGFloat(M_PI_2)
        // some variation in angle
        //子旋转角度范围
        snowflake.spinRange = CGFloat(M_PI_4)
        // slow spin
        //粒子图片
        snowflake.contents = (UIImage(named: "DazFlake")!.CGImage! as AnyObject)
        snowflake.contentsScale = 10
        //粒子颜色
        snowflake.color = UIColor.whiteColor().CGColor
        //设置阴影
        snowEmitter.shadowOpacity = 1.0
        snowEmitter.shadowRadius = 0.0
        snowEmitter.shadowOffset = CGSize(width: 0.0, height: 1.0)
        snowEmitter.shadowColor = UIColor.whiteColor().CGColor
        // 将粒子添加到粒子发射器上
        snowEmitter.emitterCells = [snowflake]
        GamePlayTableView.layer.insertSublayer(snowEmitter, atIndex: 0)
    }

水波纹动画

    func starButtonAnimation()  {
       startButtonAnimation.layer.backgroundColor = UIColor.clearColor().CGColor
        let pulseLayer = CAShapeLayer.init()
        pulseLayer.frame = startButtonAnimation.layer.bounds;
        pulseLayer.frame.size.width = pulseLayer.frame.size.height
        pulseLayer.frame.origin.x = self.view.frame.size.width/2 - pulseLayer.frame.size.width/2

        pulseLayer.path = UIBezierPath.init(ovalInRect: pulseLayer.bounds).CGPath
        pulseLayer.fillColor = UIColor.redColor().CGColor//填充色
        pulseLayer.opacity = 0.0;

        let replicatorLayer = CAReplicatorLayer.init()
        replicatorLayer.frame = startButtonAnimation.bounds;
        replicatorLayer.instanceCount = 5;//创建副本的数量,包括源对象。
        replicatorLayer.instanceDelay = 2;//复制副本之间的延迟
        replicatorLayer.addSublayer(pulseLayer)
        startButtonAnimation.layer.addSublayer(replicatorLayer)

        let opacityAnima = CABasicAnimation.init(keyPath: "opacity")
        opacityAnima.fromValue = (0.3);
        opacityAnima.toValue = (0.0);

        let scaleAnima = CABasicAnimation(keyPath: "transform")
        scaleAnima.fromValue = NSValue(CATransform3D: CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0))
        scaleAnima.toValue = NSValue(CATransform3D: CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0))
        let groupAnima = CAAnimationGroup()
        groupAnima.animations = [opacityAnima, scaleAnima]
        groupAnima.duration = 10.0
        groupAnima.autoreverses = false
        groupAnima.repeatCount = HUGE
        pulseLayer.addAnimation(groupAnima, forKey: "groupAnimation")
    }

你可能感兴趣的:(IOS精益编程)