在 Swift 中使用 IBAction 但不使用 viewDidLoad 的函数
Function working with IBAction but not with viewDidLoad in Swift
我想在我的项目中添加一个圆形动画。我找到 very good help here.
此代码适用于我的主页 viewDidLoad,但不适用于其他页面。
但是,如果我在所需页面上放置一个测试按钮,它就会起作用。当我调用viewDidLoad中的函数时,动画已经结束了,即使我放10秒或60秒。
这是代码:
import Foundation
import UIKit
class CircleView: UIView{
var circleLayer: CAShapeLayer!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
// Use UIBezierPath as an easy way to create the CGPath for the layer.
// The path should be the entire circle.
let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)
// Setup the CAShapeLayer with the path, colors, and line width
circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.strokeColor = UIColor.white.cgColor
circleLayer.lineWidth = 2.0;
// Don't draw the circle initially
circleLayer.strokeEnd = 0.0
// Add the circleLayer to the view's layer's sublayers
layer.addSublayer(circleLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func animateCircle(_ duration: TimeInterval) {
// We want to animate the strokeEnd property of the circleLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Set the animation duration appropriately
animation.duration = duration
// Animate from 0 (no circle) to 1 (full circle)
animation.fromValue = 1
animation.toValue = 0
// Do a linear animation (i.e. the speed of the animation stays the same)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// Set the circleLayer's strokeEnd property to 1.0 now so that it's the
// right value when the animation ends.
circleLayer.strokeEnd = 0.0
// Do the actual animation
circleLayer.add(animation, forKey: "animateCircle")
}
}
在我看来:
func addCircleView() {
let diceRoll = CGFloat(100)
let circleWidth = CGFloat(200)
let circleHeight = circleWidth
// Create a new CircleView
let circleView = CircleView(frame: CGRect(diceRoll, 0, circleWidth, circleHeight))
view.addSubview(circleView)
// Animate the drawing of the circle over the course of 1 second
circleView.animateCircle(TimeInterval(seconds))
}
(seconds 是一个实例变量,我想在视图打开时调用 addCircleView)。
viewDidLoad 在实例化 UIViewController 时被调用。显示 UIViewController 时调用 viewDidAppear。您需要将动画移动到 viewDidAppear
我想在我的项目中添加一个圆形动画。我找到 very good help here.
此代码适用于我的主页 viewDidLoad,但不适用于其他页面。 但是,如果我在所需页面上放置一个测试按钮,它就会起作用。当我调用viewDidLoad中的函数时,动画已经结束了,即使我放10秒或60秒。
这是代码:
import Foundation
import UIKit
class CircleView: UIView{
var circleLayer: CAShapeLayer!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
// Use UIBezierPath as an easy way to create the CGPath for the layer.
// The path should be the entire circle.
let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 10)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)
// Setup the CAShapeLayer with the path, colors, and line width
circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.strokeColor = UIColor.white.cgColor
circleLayer.lineWidth = 2.0;
// Don't draw the circle initially
circleLayer.strokeEnd = 0.0
// Add the circleLayer to the view's layer's sublayers
layer.addSublayer(circleLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func animateCircle(_ duration: TimeInterval) {
// We want to animate the strokeEnd property of the circleLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Set the animation duration appropriately
animation.duration = duration
// Animate from 0 (no circle) to 1 (full circle)
animation.fromValue = 1
animation.toValue = 0
// Do a linear animation (i.e. the speed of the animation stays the same)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// Set the circleLayer's strokeEnd property to 1.0 now so that it's the
// right value when the animation ends.
circleLayer.strokeEnd = 0.0
// Do the actual animation
circleLayer.add(animation, forKey: "animateCircle")
}
}
在我看来:
func addCircleView() {
let diceRoll = CGFloat(100)
let circleWidth = CGFloat(200)
let circleHeight = circleWidth
// Create a new CircleView
let circleView = CircleView(frame: CGRect(diceRoll, 0, circleWidth, circleHeight))
view.addSubview(circleView)
// Animate the drawing of the circle over the course of 1 second
circleView.animateCircle(TimeInterval(seconds))
}
(seconds 是一个实例变量,我想在视图打开时调用 addCircleView)。
viewDidLoad 在实例化 UIViewController 时被调用。显示 UIViewController 时调用 viewDidAppear。您需要将动画移动到 viewDidAppear