2014-09-20 63 views
0

我想把這個例子http://lepetit-prince.net/ios/?p=1510翻譯成RubyMotion,我創建的'餅片'都沒有顯示在屏幕上。我沒有在示例中使用for循環,因爲我只使用餅圖的兩個「切片」。有任何想法嗎?RubyMotion餅圖CALayer

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) 
+0

嘗試移動''@ window.addSubview(圖) ''到最後一行 – ahmet 2014-09-20 22:59:34

+0

不幸伊利沒有工作 – 2014-09-21 01:07:46

回答

0

我得到你的代碼,稍作修改。

  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 
+0

歡迎來到堆棧溢出!如果我的回答對您有幫助,請在答案旁邊勾選複選標記。 – vacawama 2014-09-21 16:21:22