我正在尝试获取单个应用程序的总体CPU使用率。我找到了一些资源,但是它们要么是用C编写的,要么是过时的。谁能帮助我解决这个问题?我正在尝试将此 https://github.com/beltex/SystemKit/blob/master/SystemKit/System.swift#L12转换 为快速。
到现在为止我已经可以转换这么多
fileprivate func hostCPULoadInfo() -> host_cpu_load_info{
let HOST_CPU_LOAD_INFO_COUNT = MemoryLayout<host_cpu_load_info>.stride / MemoryLayout<integer_t>.stride;
var size = mach_msg_type_number_t(HOST_CPU_LOAD_INFO_COUNT);
var hostInfo = host_cpu_load_info_t.allocate(capacity: 1);
let result = withUnsafeMutablePointer(to: &hostInfo) {$0.withMemoryRebound(to: integer_t.self, capacity: Int(size)){
host_info(mach_host_self(), Int32(HOST_BASIC_INFO), $0, &size)
}
}
let data = hostInfo.move()
hostInfo.deallocate(capacity: 1)
#if DEBUG
if result != KERN_SUCCESS{
print("Error - \(#file): \(#function) - kern_result_t = \(result)");
}
#endif
return data;
}
public func cpuUsage() -> (system: Double, user: Double, idle : Double, nice: Double){
let load = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在视图控制器中制作一个动画,其中圆随着动画旋转。圆应该旋转直到完成以下gif所示的过程。我已经实现了圆形动画,但无法达到想要实现的目标。
import UIKit
class ViewController: UIViewController {
var circle : Circle?;
override func viewDidLoad() {
super.viewDidLoad();
view.backgroundColor = UIColor.white;
setupViews();
}
func setupViews(){
circle = Circle(frame: self.view.frame);
view.addSubview(circle!);
circle?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true;
circle?.topAnchor.constraint(equalTo: view.topAnchor).isActive = true;
circle?.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true;
circle?.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true;
}
}
class Circle : UIView{
override init(frame: CGRect) {
super.init(frame: frame);
self.backgroundColor = .blue;
self.translatesAutoresizingMaskIntoConstraints = false;
setupCircle();
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let gradientLayer = CAGradientLayer();
func …Run Code Online (Sandbox Code Playgroud)