Swift进度视图完成操作

时间:2015-08-12 13:53:08

标签: swift

Helllo,我正在使用swift。我有这个代码控制我的主视图控制器上的进度视图。我使用包含秒的double var提供进度视图。一旦进度视图完成,我想在主视图控制器上执行一些操作。但我不知道在哪里以及如何实施行动。这是我的代码:

class CounterProgressView: UIView {

    let sharedDefaults = NSUserDefaults(suiteName: "group.birkyboy.TodayExtensionSharingDefaults")

    private let progressLayer: CAShapeLayer = CAShapeLayer()

    private var progressLabel: UILabel

    required init(coder aDecoder: NSCoder) {
        progressLabel = UILabel()
        super.init(coder: aDecoder)
        createProgressLayer()
    }

    override init(frame: CGRect) {
        progressLabel = UILabel()
        super.init(frame: frame)
        createProgressLayer()
    }

    private func createProgressLayer() {
        let startAngle = CGFloat(M_PI_2)
        let endAngle = CGFloat(M_PI * 2 + M_PI_2)
        let centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame)/2)

        var gradientMaskLayer = gradientMask()
        progressLayer.path = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).CGPath
        progressLayer.backgroundColor = UIColor.clearColor().CGColor
        progressLayer.fillColor = UIColor.clearColor().CGColor
        progressLayer.strokeColor = UIColor.blackColor().CGColor
        progressLayer.lineWidth = 25.0
        progressLayer.strokeStart = 0.0
        progressLayer.strokeEnd = 0.0

        gradientMaskLayer.mask = progressLayer
        layer.addSublayer(gradientMaskLayer)
    }

    private func gradientMask() -> CAGradientLayer {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds

        gradientLayer.locations = [1.0, 1.0]

        let colorTop: AnyObject = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1).CGColor
        let colorBottom: AnyObject = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0).CGColor
        let arrayOfColors: [AnyObject] = [colorTop, colorBottom]
        gradientLayer.colors = arrayOfColors

        return gradientLayer
    }


    func hideProgressView() {
        progressLayer.strokeEnd = 0.0
        progressLayer.removeAllAnimations()

    }

    func secondsToHoursMinutesSeconds (seconds : Double) -> (Double, Double, Double) {
        let (hr,  minf) = modf (seconds / 3600)
        let (min, secf) = modf (60 * minf)
        return (hr, min, 60 * secf)
    }

    func  animateProgressView() {

        progressLayer.strokeEnd = 0.0

        var temps = sharedDefaults!.objectForKey("roueCounter") as? Double

        println(temps)
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = CGFloat(0.0)
        animation.toValue = CGFloat(1)
        animation.duration = temps!
        animation.delegate = self
        animation.removedOnCompletion = false
        animation.additive = true
        animation.fillMode = kCAFillModeForwards

        progressLayer.addAnimation(animation, forKey: "strokeEnd")
    }

    override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {

    }
}

这是调用progressview的主viewController上的函数:

func secondsToHoursMinutesSeconds (seconds : Int) {

    sharedDefaults!.setObject(seconds, forKey: "roueCounter")
    sharedDefaults!.synchronize()

    let (h, m, s) = (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)

    if m < 10 {
        counterInfosLabel.text = "FROM CURRENT TIME IN"
        counter.text = "\(h)H0\(m)"
        println ("\(h) Hours, 0\(m) Minutes")
        counterProgressView.animateProgressView()
    } else {
        counterInfosLabel.text = "FROM CURRENT TIME IN"
        counter.text = "\(h)H\(m)"
        println ("\(h) Hours, \(m) Minutes")
        counterProgressView.animateProgressView()
    }

    if h == 0 && (m <= 5 && m > 0){
        counter.textColor = UIColor.redColor()
        counterInfosLabel.text = "FROM CURRENT TIME IN"
        PKNotification.toastBackgroundColor = UIColor.redColor()
        PKNotification.toast("You are Illegal in 5 minutes")
        counterProgressView.animateProgressView()

        var localNotification = UILocalNotification()
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
        localNotification.alertBody = "You Are Illegal In 5 Minutes."
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.soundName = "chime.mp3"
        localNotification.category = "CATAGORY_1"
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }
    if h <= 0 && m <= 0 {
        counterInfosLabel.text = "ILLEGAL SINCE"
        counter.text = "\(-h)H\(-m)MN"
        PKNotification.toastBackgroundColor = UIColor.redColor()
        PKNotification.toast("You are Illegal")

        var localNotification = UILocalNotification()
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
        localNotification.alertBody = "You Are Illegal !"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.soundName = "chime.mp3"
        localNotification.category = "CATAGORY_1"
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }
}

感谢您提供给我的任何帮助。

2 个答案:

答案 0 :(得分:0)

因此,当动画完成时,进度视图知道该怎么做,你可以给你的CounterProgressView一个完成处理程序属性:

var completionHandler: (() -> ())?

然后修改animateProgressView接受并保存调用者提供的完成处理程序:

func animateProgressView(completionHandler: (() -> ())?) {   
    self.completionHandler = completionHandler

    // set up and start animation
}

然后你可以让动画完成委托方法调用这个闭包:

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    completionHandler?()
    completionHandler = nil
}

显然,现在当你开始动画时,你可以指定动画完成时想要发生的事情:

counterProgressView.animateProgressView() {
    // what to do when animation is done
}

答案 1 :(得分:0)

是的,我认为我确实遵循了:

import UIKit

class CounterProgressView: UIView {


let sharedDefaults = NSUserDefaults(suiteName: "group.birkyboy.TodayExtensionSharingDefaults")

var completionHandler: (() -> ())?

private let progressLayer: CAShapeLayer = CAShapeLayer()

private var progressLabel: UILabel

required init(coder aDecoder: NSCoder) {
    progressLabel = UILabel()
    super.init(coder: aDecoder)
    createProgressLayer()


}

override init(frame: CGRect) {
    progressLabel = UILabel()
    super.init(frame: frame)
    createProgressLayer()

}



private func createProgressLayer() {
    let startAngle = CGFloat(M_PI_2)
    let endAngle = CGFloat(M_PI * 2 + M_PI_2)
    let centerPoint = CGPointMake(CGRectGetWidth(frame)/2 , CGRectGetHeight(frame)/2)

    var gradientMaskLayer = gradientMask()
    progressLayer.path = UIBezierPath(arcCenter:centerPoint, radius: CGRectGetWidth(frame)/2 - 30.0, startAngle:startAngle, endAngle:endAngle, clockwise: true).CGPath
    progressLayer.backgroundColor = UIColor.clearColor().CGColor
    progressLayer.fillColor = UIColor.clearColor().CGColor
    progressLayer.strokeColor = UIColor.blackColor().CGColor
    progressLayer.lineWidth = 25.0
    progressLayer.strokeStart = 0.0
    progressLayer.strokeEnd = 0.0

   gradientMaskLayer.mask = progressLayer
    layer.addSublayer(gradientMaskLayer)
}


private func gradientMask() -> CAGradientLayer {
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = bounds

    gradientLayer.locations = [1.0, 1.0]

    let colorTop: AnyObject = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1).CGColor
    let colorBottom: AnyObject = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0).CGColor
    let arrayOfColors: [AnyObject] = [colorTop, colorBottom]
    gradientLayer.colors = arrayOfColors

    return gradientLayer
}


func hideProgressView() {
    progressLayer.strokeEnd = 0.0
    progressLayer.removeAllAnimations()

}

func secondsToHoursMinutesSeconds (seconds : Double) -> (Double, Double, Double) {
    let (hr,  minf) = modf (seconds / 3600)
    let (min, secf) = modf (60 * minf)
    return (hr, min, 60 * secf)
}

func animateProgressView(completionHandler: (() -> ())?) {
    self.completionHandler = completionHandler

    progressLayer.strokeEnd = 0.0

    var temps = sharedDefaults!.objectForKey("roueCounter") as? Double

    println(temps)
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = CGFloat(0.0)
    animation.toValue = CGFloat(1)
    animation.duration = temps!
    animation.delegate = self
    animation.removedOnCompletion = false
    animation.additive = true
    animation.fillMode = kCAFillModeForwards

    progressLayer.addAnimation(animation, forKey: "strokeEnd")


}



override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    completionHandler?()
    completionHandler = nil
}

}