RubyMotion饼图CALayer

时间:2014-09-20 21:47:15

标签: ruby calayer pie-chart rubymotion

我正在尝试将此示例http://lepetit-prince.net/ios/?p=1510转换为RubyMotion,并且我创建的“饼图”都不会显示在屏幕上。我没有在示例中使用for循环,因为我只使用饼图的两个“切片”。有什么想法吗?

chart = UIView.alloc.initWithFrame(CGRect.new([60, 100], [200, 200]))
chart.backgroundColor = UIColor.colorWithRed(0, green:0, blue:0, alpha:0.5)
chart.layer.cornerRadius = 100
@window.addSubview(chart)

green = 70.0
red = 30.0

red = red / 100.0 * 2.0 * Math::PI
green = green / 100 * 2.0 * Math::PI
start = 0.0

path = UIBezierPath.alloc.init
finish = start + red
sa = start - Math::PI / 2.0
ea = finish - Math::PI / 2.0
puts sa, ea
path.moveToPoint(CGPoint.new(100, 100))
path.addArcWithCenter(CGPoint.new(100, 100), radius:100, startAngle:sa, endAngle:ea, clockwise:true)
sl = CAShapeLayer.alloc.init
sl.fillColor = UIColor.redColor
sl.path = path.CGPath
chart.layer.addSublayer(sl)

start = finish

path = UIBezierPath.alloc.init
finish = start + green
sa = start - Math::PI / 2.0
ea = finish - Math::PI / 2.0
path.moveToPoint(CGPoint.new(100, 100))
path.addArcWithCenter(CGPoint.new(100, 100), radius:100, startAngle:sa, endAngle:ea, clockwise:true)
sl = CAShapeLayer.alloc.init
sl.fillColor = UIColor.greenColor
sl.path = path.CGPath
chart.layer.addSublayer(sl)

mask = UIView.alloc.initWithFrame(CGRect.new([0, 0], [196, 196]))
mask.layer.cornerRadius = 98
mask.center = CGPoint.new(100, 100)
mask.backgroundColor = UIColor.whiteColor

chart.addSubview(mask)

1 个答案:

答案 0 :(得分:0)

我让您的代码可以进行微小的修改。

  1. 唯一真正的错误是CALayer的填充颜色需要是CGColor而不是UIColor。为了解决这个问题,我添加了.CGColor来进行转换。

  2. 我将您的代码放在viewDidLoad的{​​{1}}内。由于您没有提供所有代码,因此我不确定您是如何实现它的。

  3. 在RubyMotion中使用ViewControllerCGPoint.new是不必要的。我用更简单的直接数组常量替换了那些,例如CGRect.new[100, 100]

  4. 我将面具缩小,使其看起来更像原始示例。

  5. 这里是完整的代码:

    <强> app_delegate.rb

    [[60, 100], [200, 200]]

    <强> view_controller.rb

    class AppDelegate
      def application(application, didFinishLaunchingWithOptions:launchOptions)
        @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
        @window.rootViewController = ViewController.alloc.init
        @window.makeKeyAndVisible
        true
      end
    end