У меня есть виджет с высотой 110. Я программно делаю круг, используя UIBezierPath.У меня есть видение в виджете ClockView (ширина и высота 80), которое центрировано по оси X, и его центр также должен быть центром круга.Я смог добиться этого, как вы увидите на снимке экрана симулятора, но при работе на реальном устройстве он полностью выключен.
let centerY = 15 + clockView.frame.size.height/2
let centerX = 15 + clockView.frame.size.width/2
let centerPoint = CGPoint(x: centerX, y: centerY)
let circularPath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
Скриншот симулятора
снимок экрана iPhone
Раскадровка
Полный код настройки вида
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.refresh) , userInfo: nil, repeats: true)
circleRadius = CGFloat(2*(clockView.frame.size.height/2)/2.4)
lineWidth = Double(circleRadius/5)
//view.backgroundColor = UIColor(red:0.95, green:0.97, blue:1.00, alpha:1.0)
let trackLayer = CAShapeLayer()
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
let padding = (height - clockView.frame.size.width)/2
print("PADDING = \(padding)")
let centerY = 15 + clockView.frame.size.height/2
let centerX = 15 + clockView.frame.size.width/2
let centerPoint = CGPoint(x: centerX, y: centerY)
let circularPath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor(red:0.92, green:0.96, blue:1.00, alpha:1.0).cgColor
trackLayer.lineWidth = CGFloat(lineWidth)
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
trackLayer.shadowColor = UIColor(red:0.46, green:0.57, blue:0.68, alpha:1.0).cgColor
trackLayer.shadowRadius = 13
trackLayer.shadowOffset = CGSize(width: 0, height: 0)
trackLayer.shadowOpacity = 0.3
view.layer.addSublayer(trackLayer)
completeLayer.path = circularPath.cgPath
completeLayer.strokeColor = UIColor(red:0.50, green:0.73, blue:0.98, alpha:1.0).cgColor
completeLayer.lineWidth = CGFloat(lineWidth)
completeLayer.fillColor = UIColor.clear.cgColor
completeLayer.lineCap = kCALineCapRound
completeLayer.strokeEnd = 0
view.layer.addSublayer(completeLayer)
let topCirclePath = UIBezierPath(arcCenter: centerPoint, radius: CGFloat(Double(circleRadius)-Double(lineWidth)), startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
timeLayer.path = topCirclePath.cgPath
timeLayer.strokeColor = UIColor(red:0.92, green:0.96, blue:1.00, alpha:1.0).cgColor
timeLayer.lineWidth = CGFloat(lineWidth)
timeLayer.fillColor = UIColor(red:0.92, green:0.96, blue:1.00, alpha:1.0).cgColor
timeLayer.lineCap = kCALineCapRound
timeLayer.shadowRadius = 10
timeLayer.shadowOffset = CGSize(width: 0, height: 0)
timeLayer.shadowColor = UIColor.black.cgColor
timeLayer.shadowOpacity = 0.2
view.layer.addSublayer(timeLayer)
view.layer.insertSublayer(clockView.layer, above: timeLayer)
refresh()
// Do any additional setup after loading the view from its nib.
}