在SWIFT中重新初始化UIView子类

时间:2021-10-30 21:15:43

I'm trying to make a iOS screen displaying a grid, the squares of which can be resized using a slider. currently i am just using a button to test incrementing the square size.

我正在尝试制作一个显示网格的iOS屏幕,其方块可以使用滑块调整大小。目前我只是使用一个按钮来测试增加平方尺寸。

I have hacked some code from here: Swift / UIView / drawrect - how to get drawrect to update when required

我从这里破解了一些代码:Swift / UIView / drawrect - 如何在需要时获取drawrect进行更新

My problem is that, although drawrect IS being updated, I would like it to be initialised with each button press. The final response from the above link is beyond my meagre comprehension.

我的问题是,虽然更新了正确的IS,但我希望每次按下按钮都能初始化它。上述链接的最终回复超出了我的微薄理解。

Here is my code:

这是我的代码:

import UIKit

let screenSize = UIScreen.mainScreen().bounds
var screenWidth = screenSize.width
var screenHeight = screenSize.height*0.9
var squareSize: Int = 10

class ViewController: UIViewController {

// create squares and initialise it with a frame 
var squares = SquaresSubclass(frame: CGRectMake(0, 0, screenWidth, screenHeight))

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(squares)
    }

@IBAction func buttonPressed(sender: AnyObject) {
    squareSize = squareSize + 1
    squares.setNeedsDisplay()
    }
}

class SquaresSubclass: UIView {
    // initialise with the frame specified above
    override init(frame: CGRect) {
    super.init(frame: frame)
    }
    // no clue - copied from example
    required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    }
    // loop CGRects across and down the screen
    override func drawRect(rect: CGRect) {
    var xPos = 5
    let numberX = Int(0.98*screenWidth)/squareSize
    var yPos = 20
    let numberY = Int(0.96*screenHeight)/squareSize

    for var y = 0; y<numberY ; y++ {
        for var x = 0; x<numberX ; x++ {
            let color:UIColor = UIColor.yellowColor()
            let drect = CGRect(x: CGFloat(xPos), y: CGFloat(yPos), width: CGFloat(squareSize),height: CGFloat(squareSize))
            let bpath:UIBezierPath = UIBezierPath(rect: drect)
            color.set()
            bpath.stroke()
            xPos = xPos + squareSize
        }
        yPos = yPos + squareSize
        xPos = 5
        } 
    }
}

2 个解决方案

#1


0  

You're drawing more lines on top of the existing graphics. Clear the graphics context before you redraw the the grid:

您在现有图形上绘制了更多线条。在重绘网格之前清除图形上下文:

CGContextClearRect(UIGraphicsGetCurrentContext(),self.bounds)

#2


0  

Any time you want the system to call drawRect: on a view for you, you call setNeedsDisplay() on that view.

只要您希望系统调用drawRect:在视图上,您​​就可以在该视图上调用setNeedsDisplay()。

#1


0  

You're drawing more lines on top of the existing graphics. Clear the graphics context before you redraw the the grid:

您在现有图形上绘制了更多线条。在重绘网格之前清除图形上下文:

CGContextClearRect(UIGraphicsGetCurrentContext(),self.bounds)

#2


0  

Any time you want the system to call drawRect: on a view for you, you call setNeedsDisplay() on that view.

只要您希望系统调用drawRect:在视图上,您​​就可以在该视图上调用setNeedsDisplay()。