如何在单个文件中定义的多个视图控制器中使用 属性?
How to use a property in multiple view controllers defined in a single file?
我做了一个制作圆形动画的函数。函数将 radius:float 作为输入。我在 ViewController Class 中完成了此操作,并且它在该视图控制器上工作正常。但现在我想在多个视图控制器上使用这个动画,不想在每个视图控制器上编写相同的代码。所以我想知道如何在单独的文件中创建这个函数,这样我只需要用半径调用函数,它就会完成工作。或者你能告诉我这样做的最佳做法吗?
提前致谢。
//
我不想在我的 ViewController 中这样做,我只想创建一个新的 class 仅用于圆形动画。并且也不想导入 class 想要这样做-
import UIKit
class CircularProgressView {
private let shapeLayer = CAShapeLayer()
public func createProgressView(radius:CGFloat,forView:UIView) {
let center = forView.center
let circularPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: -CGFloat.pi/2, endAngle: 2*CGFloat.pi, clockwise: true)
let trackLayer = CAShapeLayer()
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineWidth = 10
forView.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 10
shapeLayer.lineCap = .round
shapeLayer.strokeEnd = 0
forView.layer.addSublayer(shapeLayer)
forView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
}
@objc private func handleTap() {
print("hello s")
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 2
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "basic")
}
}
并像这样使用-
导入 UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
CircularProgressView().createProgressView(radius: 50, forView: view)
}
}
但在此代码中手势识别器不工作。
创建一个名为 BaseViewController 的新 swift 文件
import UIKit
class BaseViewController : UIViewController {
//Add your animation code
}
并继承这个新文件
import UIKit
class YourVC : BaseViewController {
// do your stuff here
}
另一种方法是创建扩展。
extension UIViewController {
func doAnimation() {
// do your stuff here
}
}
并使用它。
view.doAnimation()
您还可以查看 Swift Documention 继承
这里是一些您可以如何做到的例子。希望对你有帮助
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func startCircularAnimation(radius: Float) {
// your animation code here
}
}
class FirstViewController: MyViewController {
override func viewDidLoad() {
super.viewDidLoad()
startCircularAnimation(radius: 25)
}
}
class SecondViewController: MyViewController {
override func viewDidLoad() {
super.viewDidLoad()
startCircularAnimation(radius: 30)
}
}
我假设您尝试调用一次函数来使用整个视图控制器。
为了实现这一点,您可以创建管理器 class。
使用 Manager.Instance.callFunction 你可以在每个控制器中使用你的动画 class.
class Manager {
private static let _instance = Manager()
static var Instance: Manager{
return _instance
}
private init(){
}
func callFunction(){
// add animation stuff.
}
您可以简单地创建一个 UIViewController
extension
并使用相关代码在其中添加一个方法 animate(with:)
,
extension UIViewController {
func animate(with radius: Float) {
//add your code here..
}
}
方法 animate(with:)
现在可用于所有 UIViewController
子类。所以,你可以简单地这样称呼它
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.animate(with: 10.0)
}
}
在这种情况下不需要创建任何父级 class
。
我做了一个制作圆形动画的函数。函数将 radius:float 作为输入。我在 ViewController Class 中完成了此操作,并且它在该视图控制器上工作正常。但现在我想在多个视图控制器上使用这个动画,不想在每个视图控制器上编写相同的代码。所以我想知道如何在单独的文件中创建这个函数,这样我只需要用半径调用函数,它就会完成工作。或者你能告诉我这样做的最佳做法吗? 提前致谢。
//
我不想在我的 ViewController 中这样做,我只想创建一个新的 class 仅用于圆形动画。并且也不想导入 class 想要这样做-
import UIKit
class CircularProgressView {
private let shapeLayer = CAShapeLayer()
public func createProgressView(radius:CGFloat,forView:UIView) {
let center = forView.center
let circularPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: -CGFloat.pi/2, endAngle: 2*CGFloat.pi, clockwise: true)
let trackLayer = CAShapeLayer()
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineWidth = 10
forView.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 10
shapeLayer.lineCap = .round
shapeLayer.strokeEnd = 0
forView.layer.addSublayer(shapeLayer)
forView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
}
@objc private func handleTap() {
print("hello s")
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 2
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "basic")
}
}
并像这样使用- 导入 UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
CircularProgressView().createProgressView(radius: 50, forView: view)
}
}
但在此代码中手势识别器不工作。
创建一个名为 BaseViewController 的新 swift 文件
import UIKit
class BaseViewController : UIViewController {
//Add your animation code
}
并继承这个新文件
import UIKit
class YourVC : BaseViewController {
// do your stuff here
}
另一种方法是创建扩展。
extension UIViewController {
func doAnimation() {
// do your stuff here
}
}
并使用它。
view.doAnimation()
您还可以查看 Swift Documention 继承
这里是一些您可以如何做到的例子。希望对你有帮助
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func startCircularAnimation(radius: Float) {
// your animation code here
}
}
class FirstViewController: MyViewController {
override func viewDidLoad() {
super.viewDidLoad()
startCircularAnimation(radius: 25)
}
}
class SecondViewController: MyViewController {
override func viewDidLoad() {
super.viewDidLoad()
startCircularAnimation(radius: 30)
}
}
我假设您尝试调用一次函数来使用整个视图控制器。 为了实现这一点,您可以创建管理器 class。 使用 Manager.Instance.callFunction 你可以在每个控制器中使用你的动画 class.
class Manager {
private static let _instance = Manager()
static var Instance: Manager{
return _instance
}
private init(){
}
func callFunction(){
// add animation stuff.
}
您可以简单地创建一个 UIViewController
extension
并使用相关代码在其中添加一个方法 animate(with:)
,
extension UIViewController {
func animate(with radius: Float) {
//add your code here..
}
}
方法 animate(with:)
现在可用于所有 UIViewController
子类。所以,你可以简单地这样称呼它
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.animate(with: 10.0)
}
}
在这种情况下不需要创建任何父级 class
。