cocoa - OSX CustomView Doesn't Work After Window Resize -


i have simple view displays nsbezierpath. on mousedown inside path, path's fill color sets yellow , view redraws. on mousedown outside path, path's fill color sets blue , view redraws.

in storyboard, have single window controller window content segue view controller. view, customview class heartview (below) fills entire view controller.

everything works fine until user resizes window vertically. after that, view exhibits bizarre behavior: mousedown no longer works everywhere inside path, recolor happens on mousedown outside path, , path (but not always) doesn't fill. think going on in superview, don't know what.

    import cocoa      class heartview: nsview {          var mouselocation : nspoint = nszeropoint          func drawobject(){             //create empty bezier path             let abezier : nsbezierpath = nsbezierpath()             abezier.movetopoint(cgpoint(x: 176.95,y: 44.90))             abezier.curvetopoint(cgpoint(x: 166.71,y: 145.89),                 controlpoint1: cgpoint(x: 76.63,y: 76.78),                 controlpoint2: cgpoint(x: 82.59,y: 206.70))             abezier.curvetopoint(cgpoint(x: 176.95,y: 44.90),                 controlpoint1: cgpoint(x: 237.55,y: 224.76),                 controlpoint2: cgpoint(x: 276.83,y: 95.98))             abezier.closepath()                     if (abezier.containspoint(nsmakepoint(mouselocation.x, mouselocation.y))){                         nscolor.yellowcolor().setfill()                         nscolor.greencolor().setstroke()                             } else {                         nscolor.bluecolor().setfill()                         nscolor.orangecolor().setstroke()                     }                     abezier.fill()                     abezier.linewidth = 2.0                     abezier.stroke()          }           override func drawrect(dirtyrect: nsrect) {             super.drawrect(dirtyrect)             drawobject()         }          override func mousedown(theevent: nsevent) {             mouselocation.x = theevent.locationinwindow.x             mouselocation.y = theevent.locationinwindow.y             self.setneedsdisplayinrect(self.frame)           } 

}

i found answer in lucas derraugh's video on mouse events (cocoa programming l27). turns out, capturing mousedown event in superview's coordinate system. in mousedown event, used "locationinwindow," caused strange behavior. changed method to:

    override func mousedown(theevent: nsevent) {          var viewpoint:nspoint = self.convertpoint(theevent.locationinwindow, fromview: nil)          mouselocation.x = viewpoint.x          mouselocation.y = viewpoint.y          self.needsdisplay = true }  

to convert window's coordinate system view's. things work after window resize event.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -