繁体   English   中英

RubyMotion饼图CALayer

[英]RubyMotion Pie Chart CALayer

我正在尝试将此示例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. 唯一真正的错误是CALayer的填充颜色必须是CGColor而不是UIColor 为了解决这个问题,我添加了.CGColor进行转换。

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

  3. 这是没有必要使用CGPoint.newCGRect.new在RubyMotion。 我将其替换为更简单的直接数组常量,例如[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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM