首页 > 解决方案 > 模拟器与真实手机中的内容变化

问题描述

我有一个高度为 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 截图 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.
}

标签: iosswiftstoryboardconstraintsuibezierpath

解决方案


第一步是仔细检查您的手机和模拟器是否是相同的 iOS 和型号。

帖子的第二件事,可能会发布更多代码,显示有问题的确切项目/元素。(即您所拥有的标签/项目,它可能缺少硬件所需的某种项目/标签/值。)

希望有帮助。


推荐阅读