2014-07-30 11 views
5

Próbuję narysować okrąg w Swift, ale kiedy piszę swój kod, otrzymuję komunikat "nie mogę znaleźć przeciążenia dla" init ", który akceptuje podane argumenty:Jak zainicjować UIBezierPath, aby narysować okrąg w szybkim tempie?

W klasie UIBezierPath jest funkcji init:

init(arcCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> UIBezierPath 

ale kiedy ogłosił to z tego kodu pojawia się błąd .. Need rzucam żadnych zmienną innego typu, ale jeśli to skompilowane w iPhone 4 nie pojawia się błąd, tylko? w iphone 5/5s. Jak można to poprawnie zgłosić?

let arcCenter = CGPoint(x: CGRectGetMidX(self.bounds), y: CGRectGetMidY(self.bounds)) 
    let radius  = Float(min(CGRectGetMidX(self.bounds) - 1, CGRectGetMidY(self.bounds)-1)) 

    let circlePath : UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: -rad(90), endAngle: rad(360-90), clockwise: true) 

Dzięki!

+1

Spróbuj to: niech circlePath: UIBezierPath = UIBezierPath (arcCenter: arcCenter, promień: CGFloat (promień), startAngle: CGFloat (-rad (90)), endAngle: CGFloat (rad (360-90)), zgodnie z ruchem wskazówek zegara: true) – azimov

+0

Dzięki! To jest poprawne! – user3745888

Odpowiedz

8

Konieczne jest przekonwertowanie wartości przekazanych jako argumenty w metodzie init UIBezierPath na CGFloat, ponieważ Swift widzi je jako Double lub Float (niech promień).

let circlePath : UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: 
CGFloat(radius), startAngle: CGFloat(-rad(90)), endAngle: CGFloat(rad(360-90)), clockwise: true) 
+1

Nie rozumiem -rad w powyższym stwierdzeniu. Czy to jest zadeklarowany var? Jaka jest wartość? – zeeple

+1

Myślę, że pobyt w radach dla DegreesToRadians -> func rad (value: Double) -> Double { return value * M_PI/180.0 } –

0

Swift 3:

let circlePath = UIBezierPath(arcCenter: CGPoint.zero, radius: radius, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 
0

Można copypasted do zabaw:

import UIKit 
class Circle: UIView { 
    var strokeColor: UIColor 
    var fillColor: UIColor 
    init(frame: CGRect, strokeColor: UIColor, fillColor: UIColor = .clear) { 
     self.strokeColor = strokeColor 
     self.fillColor = fillColor 
     super.init(frame: frame) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
    override func draw(_ rect: CGRect) { 
     let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.width/2, y: frame.height/2), radius: frame.height/2, startAngle: CGFloat(0), endAngle: CGFloat.pi * 2, clockwise: true) 
     strokeColor.setStroke() 
     fillColor.setFill() 
     circlePath.lineWidth = 1 
     circlePath.stroke() 
    } 

} 


let circle = Circle(frame: CGRect(x: 0, y: 0, width: 100, height: 100), strokeColor: .red, fillColor: .blue) 
Powiązane problemy