我有一个高度为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)
完整的视图设置代码
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.
}
答案 0 :(得分:0)
第一步是要仔细检查您的手机和模拟器是否为相同的iOS和型号。
帖子的第二件事,可能是发布更多代码,以显示有问题的确切项目/元素。 (即您所拥有的标签/项目,可能缺少硬件上所需的某种项目/标签/值。)
希望有帮助。
答案 1 :(得分:0)
为解决此问题,我使中心坐标等于ClockView的宽度和高度除以2。我还将圆圈添加为ClockView层的一部分,而不是主层。