将视图添加为子视图,并希望它在网格化的另一个视图周围拖动。为了使它保持在网格线上,它需要捕捉到网格规格。(10px)。
我有这个,但它基于我在视图上单击鼠标而不是视图大小本身。
- (void)mouseDragged:(NSEvent *)theEvent
{
float step = 10.0; // Grid step size.
NSPoint newDragLocation = [theEvent locationInWindow];
newDragLocation.x = step * floor((newDragLocation.x / step) + 0.5);
newDragLocation.y = step * floor((newDragLocation.y / step) + 0.5);
NSLog(@"%f,%f",newDragLocation.x,newDragLocation.y);
NSPoint thisOrigin = [self frame].origin;
thisOrigin.x += (-lastDragLocation.x + newDragLocation.x);
thisOrigin.y += (-lastDragLocation.y + newDragLocation.y);
[self setFrameOrigin:thisOrigin];
lastDragLocation = newDragLocation;
}
需要基于视图大小,因为我添加了需要对齐的多个视图。谢谢!