创建网格线并允许用户打开/关闭网格线视图

发布于 2024-12-02 17:04:28 字数 303 浏览 1 评论 0原文

我想在我的应用程序中创建网格线方法,并允许用户打开/关闭网格视图。它不需要任何类型的触摸检测或与之相关的逻辑。只是网格线供用户查看并打开/关闭它们。显而易见的解决方案是将网格图像添加到我的视图中,并在需要时使用图像视图显示它。但这是我不能接受的选择。我必须以编程方式完成它。感谢您抽出时间。我计划实施的图片 。有什么想法吗?核心显卡还是很多 uiview?

I would like to create a grid line approach in my app and allow the user to switch on/off the grid view. It doesn't need to have any sort of touch detection or a logic associated to it. Just grid lines for the user to see and turn them on/off. The obvious solution for this would be to add an image of grid to my view and display it whenever required using an imageview. But that is an option that I cannot take. I have to do it programmatically. Thanks for your time. An image of what I am planning to implement. Any ideas ? Core graphics or many uiviews ?

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

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

发布评论

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

评论(3

柠檬色的秋千 2024-12-09 17:04:28

我已将其用于 Swift3

class GridView: UIView {

    var numberOfColumns: Int = 2
    var numberOfRows: Int = 2
    var lineWidth: CGFloat = 1.0
    var lineColor: UIColor = UIColor.white

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {

            context.setLineWidth(lineWidth)
            context.setStrokeColor(UIColor.white.cgColor)

            let columnWidth = Int(rect.width) / (numberOfColumns + 1)
            for i in 1...numberOfColumns {
                var startPoint = CGPoint.zero
                var endPoint = CGPoint.zero
                startPoint.x = CGFloat(columnWidth * i)
                startPoint.y = 0.0
                endPoint.x = startPoint.x
                endPoint.y = frame.size.height
                context.move(to: CGPoint(x: startPoint.x, y: startPoint.y))
                context.addLine(to: CGPoint(x: endPoint.x, y: endPoint.y))
                context.strokePath()
            }

            let rowHeight = Int(rect.height) / (numberOfRows + 1)
            for j in 1...numberOfRows {
                var startPoint = CGPoint.zero
                var endPoint = CGPoint.zero
                startPoint.x = 0.0
                startPoint.y = CGFloat(rowHeight * j)
                endPoint.x = frame.size.width
                endPoint.y = startPoint.y
                context.move(to: CGPoint(x: startPoint.x, y: startPoint.y))
                context.addLine(to: CGPoint(x: endPoint.x, y: endPoint.y))
                context.strokePath()
            }
        }
    }
}

并将其背景颜色设置为 clear

该代码也位于此处

I have used this for Swift3

class GridView: UIView {

    var numberOfColumns: Int = 2
    var numberOfRows: Int = 2
    var lineWidth: CGFloat = 1.0
    var lineColor: UIColor = UIColor.white

    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {

            context.setLineWidth(lineWidth)
            context.setStrokeColor(UIColor.white.cgColor)

            let columnWidth = Int(rect.width) / (numberOfColumns + 1)
            for i in 1...numberOfColumns {
                var startPoint = CGPoint.zero
                var endPoint = CGPoint.zero
                startPoint.x = CGFloat(columnWidth * i)
                startPoint.y = 0.0
                endPoint.x = startPoint.x
                endPoint.y = frame.size.height
                context.move(to: CGPoint(x: startPoint.x, y: startPoint.y))
                context.addLine(to: CGPoint(x: endPoint.x, y: endPoint.y))
                context.strokePath()
            }

            let rowHeight = Int(rect.height) / (numberOfRows + 1)
            for j in 1...numberOfRows {
                var startPoint = CGPoint.zero
                var endPoint = CGPoint.zero
                startPoint.x = 0.0
                startPoint.y = CGFloat(rowHeight * j)
                endPoint.x = frame.size.width
                endPoint.y = startPoint.y
                context.move(to: CGPoint(x: startPoint.x, y: startPoint.y))
                context.addLine(to: CGPoint(x: endPoint.x, y: endPoint.y))
                context.strokePath()
            }
        }
    }
}

And set its background colour as clear.

The code is also present here

回眸一笑 2024-12-09 17:04:28

子类化 UIView 并在其中创建单独的 2 个循环。一种用于垂直线,一种用于水平线。
在垂直线循环中,创建 UIView 的 1px 宽和 768px 高。在水平方向上,将它们创建为 1 像素高和 1024 像素宽。

要隐藏和显示它,只需将子类视图的隐藏属性切换为 YES 或 NO 即可。

您也可以不使用子类化,只需使用标准 UIView。

Subclass a UIView and create separate 2 loops in there. One for vertical lines and one for horizontal lines.
In the vertical lines loop, create UIView’s 1px wide and 768px high. In horizontal one, create them 1px high and 1024px wide.

To hide and show it, just switch the subclassed view’s hidden property to either YES or NO.

You can possibly do without the subclassing as well, just use a standard UIView.

零度° 2024-12-09 17:04:28

最终使用了这个:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sample.png"]];

Ended up using this:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sample.png"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文