快速进度 View 完成操作

标签 swift

你好,我用的是swift。我有这段代码可以控制我的主视图 Controller 上的进度 View 。我用一个包含秒数的双变量来提供进度 View 。进度 View 完成后,我想在主视图 Controller 上执行一些操作。但我不知道在哪里以及如何实现行动。这是我的代码:

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)
    }
}

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

最佳答案

所以进度 View 知道动画完成时要做什么,你可以给你的 CounterProgressView 一个完成处理程序属性:

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

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

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

    // set up and start animation
}

然后您可以让动画完成委托(delegate)方法调用此闭包:

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

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

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

关于快速进度 View 完成操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967253/

相关文章:

ios - Swift IOS 9 - 以最简单的方式实现应用内购买

arrays - Array<SomeStruct> 上的操作比 Array<SomeClass> 上的操作慢...为什么?

xcode - 快速获取 UITableViewCell 内单元格的索引路径

swift - 两点之间的正弦波 UIBezierPath

swift - 获取 SKNode 父节点中第一个和最后一个子节点的属性

ios - 呈现 navigationController 时出错

ios - Siren库如何获取当前的应用商店版本?

ios - NSURLSessionDataTask 是否在后台暂停?

c++ - 使用 C++ 并作为框架编译的 Cocoapods 规范

iOS 其他应用中的点击/拖动事件