Swift 快速进度视图完成操作
喂,我用的是斯威夫特。我有这段代码控制主视图控制器上的progressview。我用一个包含秒数的双变量来填充进度视图。progressview完成后,我想在主viewcontroller上执行一些操作。但我不知道在哪里以及如何实施行动。这是我的密码:Swift 快速进度视图完成操作,swift,Swift,喂,我用的是斯威夫特。我有这段代码控制主视图控制器上的progressview。我用一个包含秒数的双变量来填充进度视图。progressview完成后,我想在主viewcontroller上执行一些操作。但我不知道在哪里以及如何实施行动。这是我的密码: class CounterProgressView: UIView { let sharedDefaults = NSUserDefaults(suiteName: "group.birkyboy.TodayExtensionShari
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) {
}
}
这是主viewController上调用progressview的函数:
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)
}
}
func secondstohoursminuteseconds(秒:Int){
sharedDefaults!.setObject(秒,forKey:“roueCounter”)
sharedDefaults!.synchronize()
设(h,m,s)=(秒/3600,(秒%3600)/60,(秒%3600)%60)
如果m<10{
counterInfosLabel.text=“从当前时间开始”
counter.text=“\(h)H0\(m)”
println(“\(h)小时,0\(m)分钟”)
counterProgressView.animateProgressView()
}否则{
counterInfosLabel.text=“从当前时间开始”
counter.text=“\(h)h\(m)”
打印项次(“小时,分钟”)
counterProgressView.animateProgressView()
}
如果h==0&(m0){
counter.textColor=UIColor.redColor()
counterInfosLabel.text=“从当前时间开始”
PKNotification.toastBackgroundColor=UIColor.redColor()
PKNotification.toast(“您在5分钟内是非法的”)
counterProgressView.animateProgressView()
var localNotification=UILocalNotification()
localNotification.fireDate=NSDate(timeIntervalSinceNow:1)
localNotification.alertBody=“您在5分钟内是非法的。”
localNotification.timeZone=NSTimeZone.defaultTimeZone()
localNotification.soundName=“chime.mp3”
localNotification.category=“CATAGORY\u 1”
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
如果h因此进度视图知道动画完成时要做什么,则可以为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
}
因此,进度视图知道动画完成时要做什么,您可以为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
}
是的,我想我确实做到了:
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
}
}是的,我想我确实做到了:
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
}
}那么,您如何知道进度何时完成?那么,您如何知道进度何时完成?这很有意义,必须在进度视图控制器类中实现?所有内容(除了调用带有闭包的animateProgressView
的最后一步)都在CounterProgressView
类中。只有视图控制器启动动画的最后一步发生在视图控制器中counterProgressView.animateProgressView(){PKNotification.toastBackgroundColor=UIColor.redColor()PKNotification.toast(“您是非法的”)}}}但是xcode给了我这个错误:不能用类型为“(()->”的参数列表调用“animateProgressView”,发生了什么?我不熟悉这个PKNotification
类,也不熟悉您的其他逻辑,因此我在这里没有任何有意义的贡献。但我可能会建议一个更具代表性的测试:print(“启动动画…”);animateProgressView(){print(“您是非法的”)}
并查看(a)您是否达到了这段代码(因为您在几个地方启动了动画);(b)动画制作完成后,会显示“您是非法的”消息。将基本动画完成逻辑与闭包中的操作细节隔离开来。您是否像我说的那样更改了animateProgressView
方法声明和实现,添加了completionHandler
参数(并更改方法以保存完成处理程序)?这很有意义,这必须在progress view controller类中实现?所有内容(除了调用带有闭包的animateProgressView
的最后一步)都在CounterProgressView
类中。只有视图控制器启动动画的最后一步发生在视图控制器中counterProgressView.animateProgressView(){PKNotification.toastBackgroundColor=UIColor.redColor()PKNotification.toast(“您是非法的”)}}}但是xcode给了我这个错误:不能用类型为“(()->”的参数列表调用“animateProgressView”,发生了什么?我不熟悉这个PKNotification
类,也不熟悉您的其他逻辑,因此我在这里没有任何有意义的贡献。但我可能会建议一个更具代表性的测试:print(“启动动画…”);animateProgressView(){print(“你是非法的”)}
并查看i