인터넷 속도 테스트 앱을 개발 중입니다. 미래의 프로젝트에 대해 더 연습하고 배우기 위해 할 일.setLineDash 대칭 Swift 2/3에
이import UIKit
@IBDesignable
class Arc: UIView {
@IBInspectable var dashWidth: CGFloat = 12.0
@IBInspectable var smallDashWidth: CGFloat = 5.0
override func draw(_ rect: CGRect) {
// Position and Radius of Arc
let center = CGPoint(x: bounds.width/2, y: bounds.height/2)
// Calculate Angles
let π = CGFloat(M_PI)
let startAngle = 3 * π/4
let endAngle = π/4
let radius = max(bounds.width/1.15, bounds.height/1.15)/2 - dashWidth/2
// Start Context
let context = UIGraphicsGetCurrentContext()
// MARK: Base
let arc = UIBezierPath(arcCenter: center, radius: radius,startAngle: startAngle,endAngle: endAngle,clockwise: true)
arc.addArc(withCenter: center, radius: radius, startAngle: endAngle, endAngle: startAngle, clockwise: false)
arc.lineJoinStyle = .bevel
arc.lineCapStyle = .round
arc.close()
UIColor.yellow().setStroke()
arc.lineWidth = smallDashWidth
//context!.saveGState()
context!.setLineDash(phase: 0, lengths: [0, 0], count: 2)
arc.stroke()
context!.saveGState()
// MARK: dash Arc
let dashArc = UIBezierPath()
dashArc.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
// Round Line
dashArc.lineJoinStyle = .round;
// Set Stroke and Width of Dash
UIColor.white().setStroke()
dashArc.lineWidth = dashWidth
// Save Context and Set Line Dash
context!.saveGState()
context!.setLineDash(phase: 0, lengths: [2, 54], count: 2)
// Draw Line
dashArc.stroke()
// Restore Context
context!.restoreGState()
}
}
결과는 이것이다 :
이 내 스위프트 코드입니다
을 내가 자동화해야 내가해야 할 일은
context!.setLineDash(phase: 0, lengths: [2, 54], count: 2)
길이는 [2, 54]이며, 숫자는 계산없이 추가되며이 공식을 얻기 위해 최종 방정식 번호를 얻는 데에만 사용됩니다. 1 : 대시 12 (나중에 변수로 할당 될 수 있음)를 호 전체에 추가해야합니다. 호는 가변 각도로 시작하고 끝납니다 (나중에 변경할 수 있음). 2 : dashArc.lineWidth = dashWidth
의 값도 변경할 수 있으며 12 개의 대시 사이의 공백을 계산하는 데 중요한 항목입니다. 3 : 제시된 모든 변수가 가변적 일 수 있으므로이 계산을 수행하는 가장 좋은 방법입니다. 4 첫 번째와 마지막 대시는 각각의 startAngle 및 endAngle과 동일한 각도 여야합니다.
내가 필요한 것 :
나는 외모와 아크 동안 가능한 한 대칭 대시를 확산 계산을해야합니다.
나는이 유사한 계산의 생각 :var numberOfDashes = 12
var perimeterArc = ?
var widthDash = 2
spacing = (perimeterArc - (widthDash * numberOfDashes))/numberOfDashes
context!.setLineDash(phase: 0, lengths: [widthDash, spacing], count: 2)
하지만 난 perimeterArc을 계산하는 방법을 모르겠어요. 누군가 나를 도울 수 있습니까? 스위프트 2/3에서 이것에 대한 논리적 인 계산을 만들 수있는 어떤 것도 생각할 수 없었습니다.
나는 어떤 조언을 주셔서 감사합니다.
감사합니다. 내가 필요한 것! – James