iOS SDK Core Graphics 游戏对象旋转?

发布于 2024-10-09 08:55:41 字数 43 浏览 2 评论 0原文

如何在核心图形中旋转矩形或圆形等对象?是否可以使用核心动画来做到这一点?

How can I rotate objects like a rectangle or circle in core graphics? and is it possible to do this with Core Animation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-10-16 08:55:41

尝试设置视图的变换属性(可设置动画)或使用 CGAffineTransformMakeRotation 进行变换,以便在绘图时应用于上下文。

Try setting the transform property of the view (which is animatable) or use CGAffineTransformMakeRotation to make a transform you can apply to your context when drawing.

栀子花开つ 2024-10-16 08:55:41
extension Double {
    var degreesToRadians: CGFloat {
        return CGFloat(self) * CGFloat(M_PI) / CGFloat(180.0)
    }
}
class Rectangle: UIView {
    override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        let path = UIBezierPath(rect: rect.insetBy(dx: 90, dy: 100))
        context?.translateBy(x: rect.midX, y: -100)
        context?.rotate(by: 45.0.degreesToRadians)
        context?.setStrokeColor(UIColor.green.cgColor)
        context?.setLineWidth(5.0)
        context?.addPath(path.cgPath)
        context?.strokePath()
    }
}

矩形旋转 45 度。

添加动画

class Rectangle: UIView {
        override func draw(_ rect: CGRect) {
            let path = UIBezierPath(rect: rect.insetBy(dx: 90, dy: 100))

            // Add shape layer
            let rectangle = CAShapeLayer()
            rectangle.frame = bounds
            rectangle.path = path.cgPath
            rectangle.strokeColor = UIColor.yellow.cgColor
            layer.addSublayer(rectangle)

            // create transform
            let transform = CATransform3DMakeRotation(90.0.degreesToRadians, 0, 0, 1)

            // Animate the transform
            let anim = CABasicAnimation(keyPath: "transform.rotation.z")
            anim.fromValue = CATransform3DIdentity
            anim.toValue = transform
            anim.duration = 2.0
            anim.repeatCount = Float.infinity
            layer.add(anim, forKey: nil)
        }
    }
extension Double {
    var degreesToRadians: CGFloat {
        return CGFloat(self) * CGFloat(M_PI) / CGFloat(180.0)
    }
}
class Rectangle: UIView {
    override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        let path = UIBezierPath(rect: rect.insetBy(dx: 90, dy: 100))
        context?.translateBy(x: rect.midX, y: -100)
        context?.rotate(by: 45.0.degreesToRadians)
        context?.setStrokeColor(UIColor.green.cgColor)
        context?.setLineWidth(5.0)
        context?.addPath(path.cgPath)
        context?.strokePath()
    }
}

The rectangle is rotated by 45 degrees.

Add Animation

class Rectangle: UIView {
        override func draw(_ rect: CGRect) {
            let path = UIBezierPath(rect: rect.insetBy(dx: 90, dy: 100))

            // Add shape layer
            let rectangle = CAShapeLayer()
            rectangle.frame = bounds
            rectangle.path = path.cgPath
            rectangle.strokeColor = UIColor.yellow.cgColor
            layer.addSublayer(rectangle)

            // create transform
            let transform = CATransform3DMakeRotation(90.0.degreesToRadians, 0, 0, 1)

            // Animate the transform
            let anim = CABasicAnimation(keyPath: "transform.rotation.z")
            anim.fromValue = CATransform3DIdentity
            anim.toValue = transform
            anim.duration = 2.0
            anim.repeatCount = Float.infinity
            layer.add(anim, forKey: nil)
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文