NSAlignment object filters the movement of an object and provides haptic feedback when alignment occurs.
- macOS 10.11+
With a Force Touch trackpad, apps can produce tactile feedback to complement user actions. If your app implements alignment features, you can use the
NSAlignment class to filter object movements and provide haptic feedback to the user at appropriate times. As the user drags objects into alignment with a guide or another object, the user actually feels a physical bump as the object snaps into place.
Implementing Alignment Feedback
To implement alignment feedback in your custom alignment controller class, set up the class to receive events for tracking the movement of an object. These can be events matching the
input value of an
NSAlignment object, or events from a gesture recognizer (
NSGesture). For each event received:
Create an instance of an
NSAlignmentobject. For example:
Inform the alignment feedback filter object about the event. To do this, call one of the following methods:
Store the location of the object before it moves in response to the event. This is considered the previous location of the object.
Move the object to its new location in response to the event. This is the location where the object will reside if no alignment occurs.
Store the new location of the object. This is considered the default location of the object.
Determine where the object will move to be aligned. This is considered the aligned location of the object.
Request a feedback token based on the previous location, default location, and aligned location. To do this, call one of the following methods:
alignment- If the object will be moved both horizontally and vertically to become aligned.
Feedback Token For Movement(in: previous Point: aligned Point: default Point:)
alignment- If the object will be moved horizontally only to become aligned.
Feedback Token For Horizontal Movement(in: previous X: aligned X: default X:)
alignment- If the object will be moved vertically only to become aligned.
Feedback Token For Vertical Movement(in: previous Y: aligned Y: default Y:)
If a feedback token is successfully prepared, call
performto perform the haptic feedback. Then, move the object to the aligned location.
Feedback(_: performance Time:)
If a value of
nullis returned, rather than a feedback token, then the system has determined that alignment and feedback are not appropriate. Perhaps the cursor is moving too fast or the distance to the aligned location is not significant enough to produce a visual snap. Move the object to its default location.