将Swift转换为C#以在Xamarin中使用

问题描述 投票:2回答:2

我正在编写一个Xamarin.iOS应用程序,并为我想要使用的加载微调器找到了一些Swift代码。所以我试图将Swift代码转换为C#。我希望这个问题没问题,因为我一直试图将这段代码转换成几个小时。这是Swift代码:

public class CircularProgressView: UIView {

public dynamic var progress: CGFloat = 0 {
    didSet {
        progressLayer.progress = progress
    }
}

fileprivate var progressLayer: CircularProgressLayer {
    return layer as! CircularProgressLayer
}

override public class var layerClass: AnyClass {
    return CircularProgressLayer.self
}


override public func action(for layer: CALayer, forKey event: String) -> CAAction? {
    if event == #keyPath(CircularProgressLayer.progress),
        let action = action(for: layer, forKey: #keyPath(backgroundColor)) as? CAAnimation {

        let animation = CABasicAnimation()
        animation.keyPath = #keyPath(CircularProgressLayer.progress)
        animation.fromValue = progressLayer.progress
        animation.toValue = progress
        animation.beginTime = action.beginTime
        animation.duration = action.duration
        animation.speed = action.speed
        animation.timeOffset = action.timeOffset
        animation.repeatCount = action.repeatCount
        animation.repeatDuration = action.repeatDuration
        animation.autoreverses = action.autoreverses
        animation.fillMode = action.fillMode
        animation.timingFunction = action.timingFunction
        animation.delegate = action.delegate
        self.layer.add(animation, forKey: #keyPath(CircularProgressLayer.progress))
    }
    return super.action(for: layer, forKey: event)
}
}

fileprivate class CircularProgressLayer: CALayer {
@NSManaged var progress: CGFloat
let startAngle: CGFloat = 1.5 * .pi
let twoPi: CGFloat = 2 * .pi
let halfPi: CGFloat = .pi / 2


override class func needsDisplay(forKey key: String) -> Bool {
    if key == #keyPath(progress) {
        return true
    }
    return super.needsDisplay(forKey: key)
}

override func draw(in ctx: CGContext) {
    super.draw(in: ctx)

    UIGraphicsPushContext(ctx)

    //Light Grey
    UIColor.lightGray.setStroke()

    let center = CGPoint(x: bounds.midX, y: bounds.midY)
    let strokeWidth: CGFloat = 4
    let radius = (bounds.size.width / 2) - strokeWidth
    let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: twoPi, clockwise: true)
    path.lineWidth = strokeWidth
    path.stroke()


    //Red
    UIColor.red.setStroke()

    let endAngle = (twoPi * progress) - halfPi
    let pathProgress = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle , clockwise: true)
    pathProgress.lineWidth = strokeWidth
    pathProgress.lineCapStyle = .round
    pathProgress.stroke()

    UIGraphicsPopContext()
}
}

let circularProgress = CircularProgressView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseInOut, animations: {
circularProgress.progress = 0.76
}, completion: nil)

这是我到目前为止:

public class CircularProgressView : UIView
{
    // I doubt I made these properties correctly?

    public float Progress
    {
        get
        {
            return ProgressLayer.Progress;
        }
        set
        {
            ProgressLayer.Progress = value;
        }
    }

    public CircularProgressLayer ProgressLayer { get; set; }

    public override Foundation.NSObject ActionForLayer(CALayer layer, string eventKey)
    {
        // I don't understand this part

        return base.ActionForLayer(layer, eventKey);
    }
}

public class CircularProgressLayer : CALayer
{
    public float Progress { get; set; }
    private nfloat startAngle = (nfloat)(1.5f * Math.PI);
    private nfloat twoPi = (nfloat)(2 * Math.PI);
    private double halfPi = Math.PI / 2;

    public override bool NeedsDisplay
    {
        get
        {
                return base.NeedsDisplay;
        }
    }

    public override void DrawInContext(CoreGraphics.CGContext ctx)
    {
        base.DrawInContext(ctx);

        UIGraphics.PushContext(ctx);

        UIColor.LightGray.SetStroke();

        var center = new CGPoint(x: Bounds.GetMidX(), y: Bounds.GetMidY());
        float strokeWidth = 4;
        nfloat radius = (Bounds.Size.Width / 2) - strokeWidth;
        var path = UIBezierPath.FromArc(center, radius: radius, startAngle: 0, endAngle: twoPi, clockwise: true);
        path.LineWidth = strokeWidth;
        path.Stroke();

        UIColor.Red.SetStroke();

        nfloat endAngle = (nfloat)((twoPi * Progress) - halfPi);
        var pathProgress = UIBezierPath.FromArc(center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true);
        pathProgress.LineWidth = strokeWidth;
        pathProgress.LineCapStyle = CGLineCap.Round;
        pathProgress.Stroke();

        UIGraphics.PopContext();
    }
}

我不明白有几个部分,比如这个:

if event == #keyPath(CircularProgressLayer.progress)

还有这个:

fileprivate var progressLayer: CircularProgressLayer {
    return layer as! CircularProgressLayer
}

我非常擅长C#,但我是Swift的新手。我希望这个问题没问题,因为我一直试图将这段代码转换成几个小时。

c# swift xamarin xamarin.ios calayer
2个回答
2
投票

更新:

我需要在Xamarin.iOS原型中创建@dynamic / NSManaged,最后能够通过在运行时创建它们来实现这一点,请参阅我的评论:

相关要点:

因此,虽然Xamarin.iOS不支持out-of-the-box,但如果需要,您可以在运行时创建它们。

原版的:

目前,您无法使用@dynamic生成动态属性(NSManaged / Xamarin.iOS),因此您发布的代码即使转换也无法生效。

问题:https://bugzilla.xamarin.com/show_bug.cgi?id=38823

如果手动构建CAAction / CAAnimation对象是满足您需求的有效选项,您可以查看Xamarin.iOS CustomPropertyAnimation示例应用程序:


1
投票

这是在Swift中表达关键路径的编译器友好方式。 Keypath是连续对象中的一系列属性(例如,company.manager.fullName)。如果您想了解更多信息,请阅读KVC。这个表达式产生一个String,在你的情况下,它将是“进步”。我想你可以用一个简单的比较代替它。

#keyPath(CircularProgressLayer.progress)

这是只读属性,它将layer属性公开为CircularProgressLayer的一个实例。 x as! y的意思是:如果不可能的话,将x施放到y或者坠毁。

fileprivate var progressLayer: CircularProgressLayer {
    return layer as! CircularProgressLayer
}
© www.soinside.com 2019 - 2024. All rights reserved.