0

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.

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

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.

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
        } 
    }
}
Community
  • 1
  • 1
p00p00
  • 3
  • 2
  • It's not clear what you are asking. You say "my problem is that, although `drawRect` IS being updated, I would like it to be initialized with each button press." What does that mean? Your button press changes the value of squareSize and calls setNeedsDisplay(), which should cause your drawRect to draw a different set of squares. It looks like your code should work. What is it doing wrong? – Duncan C Dec 03 '15 at 21:58
  • @DuncanC hi. thx. first couple of presses look ok, then it looks as though fresh grids are being displayed on top of existing ones. so you don't get a grid with increasingly large squares but what looks like a grid with more and more grids pile on top. – p00p00 Dec 03 '15 at 23:08

2 Answers2

0

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That was the one - working as expected now. ta v muchly. some reading in graphics context require then. – p00p00 Dec 04 '15 at 22:39
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)
farhadf
  • 1,918
  • 3
  • 19
  • 27