gpt4 book ai didi

ios - 为什么我的线不旋转? -- CGContextRotateCTM

转载 作者:搜寻专家 更新时间:2023-11-01 07:27:35 24 4
gpt4 key购买 nike

这是我使用的代码:

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

    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 的新手,我是不是误解了这段代码的工作原理?如果不是,问题是什么?

问候,布兰登

最佳答案

给你

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)
}

导致

enter image description here

解释:

首先:修正度数/弧度算术。

  • 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)
}

完整的工作 Playground :

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)

关于ios - 为什么我的线不旋转? -- CGContextRotateCTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com