【问题标题】:Why Are My Lines Not Rotating? -- CGContextRotateCTM为什么我的线不旋转? -- CGContextRotateCTM
【发布时间】:2016-01-23 13:33:27
【问题描述】:

这是我用来做的代码:

  • 保存上下文
  • 将上下文移动到矩形的中间
  • 旋转上下文
  • 从旋转的原点画一条线
  • 恢复上下文
  • 重复

    for i in 1...60 {
        CGContextSaveGState(ctx)
        CGContextTranslateCTM(ctx, rect.width / 2, 0)
        CGContextRotateCTM(ctx, 6 * CGFloat(i))
        CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
        CGContextMoveToPoint(ctx, 0, 0)
        if (i % 5 == 0) {
            CGContextSetLineWidth(ctx, 3.0)
            CGContextAddLineToPoint(ctx, 0, 20)
        }
        else {
            CGContextSetLineWidth(ctx, 2.0)
            CGContextAddLineToPoint(ctx, 0, 15)
        }
        CGContextStrokePath(ctx)
        CGContextRestoreGState(ctx)
    }
    

输出是几行,都紧紧地聚集在一起。

我是 Core Graphics 的新手,所以我只是误解了这段代码的工作原理吗?如果不是,是什么问题?

问候,布兰登

【问题讨论】:

  • 您可能必须将度数转换为弧度。在你旋转之前。含义 6 * CGFloat(i) * M_PI / 180 或更短的 CGFloat(i) * M_PI / 30
  • 我刚试过,它改变了图像,但它们仍然聚集在一起。 (这是我需要做的事情。)
  • 对未来此类问题的快速说明:截取您的代码结果(成群的行)和解释(带有“小时”行的时钟)应该发生的事情.

标签: ios swift rotation swift2 core-graphics


【解决方案1】:

给你

for i in 0..<60 {
    CGContextSaveGState(ctx)
    CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
    CGContextRotateCTM(ctx, CGFloat(Double(i) * M_PI / 30))
    CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
    CGContextTranslateCTM(ctx, 50, 0)
    CGContextMoveToPoint(ctx, 0, 0)
    if (i % 5 == 0) {
        CGContextSetLineWidth(ctx, 3.0)
        CGContextAddLineToPoint(ctx, -20, 0)
    } else {
        CGContextSetLineWidth(ctx, 2.0)
        CGContextAddLineToPoint(ctx, -10, 0)
    }
    CGContextStrokePath(ctx)
    CGContextRestoreGState(ctx)
}

导致

说明:

首先:校正的度数/弧度算术。

  • 6 * i 以度为单位
  • 需要以弧度表示 - 因此* M_PI / 180
  • 6 * i * M_PI / 180 可以简化为 i * M_PI / 30

其次:正确的几何形状。

  • 在开始时按 x 和 y 平移,时钟在其矩形中居中
  • 向右平移50
  • “向内”画一条线 2010 长。向内含义negative

没有内部翻译的替代方案

for i in 0..<60 {
    CGContextSaveGState(ctx)
    CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
    CGContextRotateCTM(ctx, CGFloat(Double(i) * M_PI / 30))
    CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
    CGContextMoveToPoint(ctx, 50, 0)
    if (i % 5 == 0) {
        CGContextSetLineWidth(ctx, 3.0)
        CGContextAddLineToPoint(ctx, 30, 0)
    } else {
        CGContextSetLineWidth(ctx, 2.0)
        CGContextAddLineToPoint(ctx, 40, 0)
    }
    CGContextStrokePath(ctx)
    CGContextRestoreGState(ctx)
}

完整的工作场所:

import UIKit
import XCPlayground

class CustomView : UIView {
    override func drawRect(rect: CGRect) {
        let ctx = UIGraphicsGetCurrentContext()

        for i in 0..<60 {
            CGContextSaveGState(ctx)
            CGContextTranslateCTM(ctx, rect.width / 2, rect.height / 2)
            CGContextRotateCTM(ctx, CGFloat(Double(i) * M_PI / 30))
            CGContextSetStrokeColorWithColor(ctx, UIColor.grayColor().CGColor)
            CGContextTranslateCTM(ctx, 50, 0)
            CGContextMoveToPoint(ctx, 0, 0)
            if (i % 5 == 0) {
                CGContextSetLineWidth(ctx, 3.0)
                CGContextAddLineToPoint(ctx, -20, 0)
            } else {
                CGContextSetLineWidth(ctx, 2.0)
                CGContextAddLineToPoint(ctx, -10, 0)
            }
            CGContextStrokePath(ctx)
            CGContextRestoreGState(ctx)
        }
    }
}

let v = CustomView(frame: CGRectMake(0,0,200,200))
v.backgroundColor = .lightGrayColor()
XCPShowView("blabla", view: v)

【讨论】:

  • 它有效,谢谢!快速的问题,1)为什么负数有效? 2) 为什么 / 30?
  • @BrandonBradley 正在写解释:再次检查。
  • 我明白了,谢谢。同样,我还是自定义图形的新手,但是,为什么你把 50 翻译到右边?
  • @BrandonBradley 这是你的时钟宽度。在此处选择时钟的宽度,或将其重构为绘图代码之外的某个参数或常量。您基本上必须对代码进行更多思考,将其插入 Playground 并使用这些值,您将很快理解它们的含义。
  • 明白。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
相关资源
最近更新 更多