[英]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)
我得到了您的代碼,可以進行少量修改。
唯一真正的錯誤是CALayer
的填充顏色必須是CGColor
而不是UIColor
。 為了解決這個問題,我添加了.CGColor
進行轉換。
我將您的代碼放在ViewController
的viewDidLoad
中。 由於您沒有提供所有代碼,因此我不確定您是如何實現的。
這是沒有必要使用CGPoint.new
和CGRect.new
在RubyMotion。 我將其替換為更簡單的直接數組常量,例如[100, 100]
和[[60, 100], [200, 200]]
。
我將面罩縮小了,使它看起來更像原始示例。
這是完整的代碼:
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.