Just call that function on your main-view. Christian Christian Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. The tracking rectangle is provided in the view's coordinate system, and the owner is the object that will receive the mouseEntered: and mouseExited: messages.
The userData parameter is any arbitrary object that will be provided as the userData object in the NSEvent object passed to the mouseEntered: and mouseExited: methods. The assumeInside parameter indicates whether the cursor should be assumed to be inside the tracking rectangle initially. The method returns a tracking tag that identifies the tracking rectangle, and the tracking tag is used to unregister the owner for tracking notifications using the method removeTrackingRect:.
An application can register tracking rectangles only for views that are currently displayed in a window. Although tracking rectangles are created and used by views, they are actually maintained by a view's window. As a result, tracking rectangles do not automatically move or resize when the view does. It is a subclass's responsibility to remove and re-register tracking rectangles when the frame of the view changes or it is inserted as a subview.
NSView also provides methods to support a common use of tracking rectangles; changing the cursor as a result of the mouse entering a rectangle. The addCursorRect:cursor: method allows you to register a rectangle using the view's coordinate system and specify the cursor that should be displayed while the mouse is over that rectangle. Cursor rectangles are volatile. When the view's window resizes, the frame or bounds of a view changes, the view is moved in the hierarchy, or the view is scrolled, the view receives a resetCursorRects message.
Subclasses should override resetCursorRects and register any required cursor rectangles and tracking rectangles in that method. The removeCursorRect:cursor: method allows you to explicitly remove a cursor rectangle that matches the provided parameters exactly. The discardCursorRects method removes all the cursor rectangles for a view. The DraggableItemView provides visual feedback that the cursor is over the draggable item by changing the cursor to the open handle.
The implementation of resetCursorRects , shown in Listing , discards all the current cursor rectangles and adds a new cursor rectangle for the draggable item's bounds. Adding a cursor rectangle for a view does not automatically restrict the cursor rectangle to the visible area of the view. You must do this yourself by finding the intersection of the proposed cursor rectangle with the view's visible rectangle.
If the resulting rectangle is not empty it should be passed as the first argument to the addCursorRect:cursor: method. You should never call resetCursorRects directly; instead send the view's window an invalidateCursorRectsForView: message, passing the appropriate view. The DraggableItemView object needs to reset its cursor rectangle each time the draggable item moves. The mouseUp: implementation shown in Listing sends the view's window an invalidateCursorRectsForView: message, passing the view itself as the parameter.
Likewise, in the version of mouseDown: that short circuits the event loop, shown in Listing , the invalidateCursorRectsForView: message is sent when the mouse-up event is detected. NSResponder also declares a number of responder actions that are triggered by key-down events. These actions map specific keystrokes to common actions. By implementing the appropriate action methods, you can bypass overriding the more complicated keyDown: method.
Your custom view should override the performKeyEquivalent: method if your view reacts to simple key equivalents. An example usage of a key equivalent is setting the Return key as the key equivalent of a button. When the user presses Return, the button acts as though it had been clicked. A subclass's implementation of performKeyEquivalent: should return YES if it has handled the key event, NO if it should be passed up the event chain.
If a view implements performKeyEquivalent: , it typically does not also implement keyDown:. The DraggableItemView class overrides the keyDown: method, shown in Listing , which allows the user to press the R key to reset the position of the draggable rectangle to the origin of the view. Listing DraggableItemView implementation of keyDown:.
A view handles the NSResponder action methods by simply implementing the appropriate method. The DraggableItemView class implements four of these methods, corresponding to the up, down, left, and right movement actions. The implementations are shown in Listing Each of the methods in Listing offset the draggable item's location in the appropriate direction using the offsetLocationByX:andY: method, passing the amount to offset the rectangle.
The vertical offset is adjusted by the offsetLocationByX:andY: implementation as appropriate if the view is flipped. After moving the rectangle, each method invalidates the cursor rectangles. This functionality could also have been implemented in keyDown: directly by examining the Unicode character of the pressed key, detecting the arrow keys, and acting accordingly.
However, using the responder action methods allow the commands to be remapped by the user. NSResponder isn't the only class that can generate events on the responder chain.
Any control that implements target-action methods can send those actions through the responder chain rather than to a specific object by connecting the control to the first responder proxy in Interface Builder and specifying the action. A detailed discussion of sending action messages through the responder chain is available in " The Responder Chain " in Cocoa Event Handling Guide. The DraggableItemView class implements the changeColor: method that is sent through the responder chain when the color is changed in a Color panel.
Listing shows the DraggableItemView implementation of changeColor:. Listing DraggableItemView implementation of changeColor:. When the Color panel is visible and an instance of the DraggableItemView class is the first responder, changing the color in the Color panel causes the rectangle to change color. Classes should provide key-value-coding-compliant accessor methods for all their public properties.
This provides a published interface to other objects that need to set the various display aspects of the view. Accessor methods also enforce good design and encapsulate memory management issues, which greatly reduces the chance of memory leaks and crashes. The DraggableItemView class implements getter and setter accessor methods for the following properties: itemColor , backgroundColor , and location. Each of the setter accessor methods test to see if the new value is different from the current value and, if it is, saves the new value and marks the view as needing to redisplay the appropriate portion.
In addition, the setLocation: method also invalidates the cursor tracking rectangle when the location changes. Can you help us improve? Resolved my issue. Clear instructions. Easy to follow. No jargon. Pictures helped. Didn't match my screen. Incorrect instructions. Too technical. Click again to start watching.
Asked by Claude Copy to clipboard Share this post. Copied to Clipboard. Add a Comment. In the specific case of the number of rows changing, though, use noteNumberOfRows changed. Posted by QuinceyMorris.
0コメント