在自定义UIView中绘制错误地放置在iOS7中但在iOS8中是正确的

时间:2015-02-17 11:56:48

标签: ios uiview

我在自定义视图中绘制戒指和圆圈。虽然这在iOS8中非常有效,但在iOS7中,绘图是在完全不同的起源上完成的。我在这里缺少什么?

在iOS8中正确显示:

iOS8

在iOS7中显示不正确:

enter image description here

离线渲染向我显示,iOS7正确识别我的帧:

enter image description here

我如何计算 arcCenter arcRadius

func prepareLayerAndShape()
{
    let size = self.frame.size.height >= self.frame.size.width ? self.frame.size.width : self.frame.size.height
    self.layer.masksToBounds = true
    self.layer.cornerRadius = size / 2
    self.arcRadius = size / 2
    self.arcCenter = CGPointMake(size / 2, size / 2)
}

drawRect

中完成的绘图
func createBackgroundRing(graphicsContext: CGContext)
{
    CGContextBeginPath(graphicsContext)
    CGContextSetStrokeColorWithColor(graphicsContext, self.delegate.shapeBackgroundColor(self))
    CGContextAddArc(graphicsContext, self.arcCenter.x, self.arcCenter.y, self.arcRadius - self.delegate.ringLineWidth(self) / 2, 0.0, CGFloat(2 * M_PI), 1)
    CGContextStrokePath(graphicsContext)
}

func createCountdownRing(graphicsContext: CGContext)
{
    CGContextBeginPath(graphicsContext)
    CGContextSetStrokeColorWithColor(graphicsContext, self.delegate.shapeForegroundColor(self))
    CGContextAddArc(graphicsContext, self.arcCenter.x, self.arcCenter.y, self.arcRadius - self.delegate.ringLineWidth(self) / 2, self.startAngle, self.endAngle, self.delegate.isClockwise(self))
    CGContextStrokePath(graphicsContext)
}

在ViewController中完成自定义视图的帧位置和大小的计算:

func frameForPosition(position: CGFloat) -> CGRect
{
    let displacementFact: CGFloat = 1.5
    let cWidth = self.viewCountdown.frame.size.width
    let cHeight = self.viewCountdown.frame.size.height
    let diff = cWidth > cHeight ? cWidth - cHeight : cHeight - cWidth
    let size = cWidth > cHeight ? cHeight : cWidth
    let x = cWidth > cHeight ? self.viewCountdown.frame.origin.x + diff / 2 : self.viewCountdown.frame.origin.x
    let y = cHeight > cWidth ? self.viewCountdown.frame.origin.y  + diff / 2 : self.viewCountdown.frame.origin.y

    let countDownRingFrame = CGRectMake(
        x + displacementFact * self.lineWidth * position,
        y + displacementFact * self.lineWidth * position,
        size - (displacementFact * 2.0 * self.lineWidth * position),
        size - (displacementFact * 2.0 * self.lineWidth * position)
    )

    return countDownRingFrame
}

旁注:我开始为iOS8编写项目编码,但之后不得不降级到iOS7。

1 个答案:

答案 0 :(得分:0)

我设法通过删除Storyboard中的ViewController和文件本身并再次添加它们来解决这个问题。显然降级是问题的根源。相同的代码现在工作正常。