Newbie programmer here. I'm trying to simulate the interaction of homes within a village.
I have a function (A) below that draws a simulated village (each rectangle stands for a house in the village). The issue I have is that I have another function (B - not shown except for small part below) (kicked off by a button press) that changes the underlying array that drives how the village looks. It does so in a for loop. So within function B, I run 100 iterations, and at each iteration, I call function (A) to redraw the village. The problem is that the app only draws the village at the end of the 100th loop. I've tried setNeedsDisplay, etc, with no luck, and I'd like to see the village drawn at each iteration so that it creates a bit of animation as rectangles will change from iter to iter.
Excerpt from Function B...
for iIter... 100 times {
drawVillage()
imageView.setNeedsDisplay() <-- TRYING TO REFRESH HERE
}
Function A...
func drawVillage() {
let xSize: Int = Int (gxRes / gxHomes); let ySize: Int = Int (gyRes / gyHomes)
let xRes = Int(gxHomes * xSize); let yRes = Int(gyHomes * ySize)
UIGraphicsBeginImageContextWithOptions(CGSize(width: xRes, height: yRes), false, 0)
let context = UIGraphicsGetCurrentContext()
var iStatus: Int
for var yHome: Int = 1; yHome <= gyHomes; ++yHome {
for var xHome: Int = 1; xHome <= gxHomes; ++xHome {
var rectangle = CGRect(x: (xHome - 1) * xSize, y: (yHome - 1) * ySize, width: xSize, height: ySize)
iStatus = gaVillageStatus[xHome][yHome]
if iStatus == 0 {
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
} else if iStatus == 1 {
CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
} else if iStatus == 2 {
CGContextSetFillColorWithColor(context, UIColor.blueColor().CGColor)
} else if iStatus == 3 {
CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor)
}
CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
CGContextSetLineWidth(context, 1)
CGContextAddRect(context, rectangle)
CGContextDrawPath(context, .FillStroke)
}
}
var img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageView.image = img
}