【问题标题】:RubyMotion Pie Chart CALayerRubyMotion 饼图 CALayer
【发布时间】:2014-09-20 21:47:14
【问题描述】:

我正在尝试将这个示例 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)

【问题讨论】:

  • 尝试将@window.addSubview(chart)移动到最后一行
  • 很遗憾没有成功

标签: ruby calayer pie-chart rubymotion


【解决方案1】:

我让您的代码只需稍作修改即可工作。

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

  2. 我将您的代码放在viewDidLoadViewController 中。由于您没有提供所有代码,我不确定您是如何实现的。

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

  4. 我将蒙版变小,使其看起来更像原始示例。

这是完整的代码:

app_delegate.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

view_controller.rb

class ViewController < UIViewController
  def viewDidLoad
    view.backgroundColor = UIColor.whiteColor

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

    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([100, 100])
    path.addArcWithCenter([100, 100], radius:100, startAngle:sa, endAngle:ea, clockwise:true)
    sl = CAShapeLayer.alloc.init
    sl.fillColor = UIColor.redColor.CGColor
    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([100, 100])
    path.addArcWithCenter([100, 100], radius:100, startAngle:sa, endAngle:ea, clockwise:true)
    sl = CAShapeLayer.alloc.init
    sl.fillColor = UIColor.greenColor.CGColor
    sl.path = path.CGPath
    chart.layer.addSublayer(sl)

    mask = UIView.alloc.initWithFrame([[0, 0], [100, 100]])
    mask.layer.cornerRadius = 50 
    mask.center = [100, 100]
    mask.backgroundColor = UIColor.whiteColor

    chart.addSubview(mask)
    view.addSubview(chart)
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2016-04-16
    相关资源
    最近更新 更多