2014-09-20 5 views
0

이 예제을 RubyMotion으로 변환하려고하는데 'pie slices'도 화면에 표시되지 않습니다. 이 예제에서는 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

+0

Unfortunat ely가 작동하지 않았다. –

답변

0

사소한 수정 작업을위한 코드가 있습니다.

  1. 유일한 버그

    CALayer의 채우기 색상은 CGColor 아닌 UIColor 할 필요가 있었다. 이 문제를 해결하기 위해 변환을 수행하기 위해 .CGColor을 추가했습니다.

  2. ViewController의 코드를 viewDidLoad 안에 넣었습니다. 모든 코드를 제공하지 않았으므로 어떻게 구현했는지 확신 할 수 없었습니다.

  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

Stack Overflow에 오신 것을 환영합니다! 내 대답이 도움이된다면, 대답 옆에있는 체크 표시를 체크하십시오. – vacawama