uiview - Swift - Using UIBezierPath in a Subview -


i'm trying make simple app. so, in part, i'm trying use subview draw circle on screen.

this view controller class:

gameview view linked in the storyboard. ballview view add in.

class gameviewcontroller: uiviewcontroller {       @iboutlet var gameview: uiview!      var ballview = gameballview()      override func viewdidload() {         super.viewdidload()          gameview.addsubview(ballview)         ballview.setneedsdisplay()         gameview.setneedsdisplay()    } }   class gameballview: uiview {      override func drawrect(rect: cgrect) {         let rectforball = cgrect(x: 50, y: 250, width: 20, height: 20)         let path = uibezierpath(ovalinrect: rectforball)         path.fill()         path.stroke()     } } 

drawrect called, reason nothing shows on screen. know did wrong?

your gameballview, ballview, has been assigned no frame. has 0 size, default being frame rect of cgrectzero. there reason why designated initializer of uiview init(frame:)! rectangle of 0 size invisible: point, infinitesimal.

thus, should giving view reasonable frame accommodate drawing; example:

 var ballview = gameballview(frame:cgrectmake(50,250,20,20)) 

then, in drawrect, draw inside bounds of ballview — remember, drawing respect ballview itself, not game view:

let rectforball = cgrect(x: 0, y: 0, width: 20, height: 20) 

or better:

let rectforball = rect // parameter `drawrect:` 

Comments