Re: "Google maps"-like zooming of NSScrollView's content view
Re: "Google maps"-like zooming of NSScrollView's content view
- Subject: Re: "Google maps"-like zooming of NSScrollView's content view
- From: Per Bull Holmen <email@hidden>
- Date: Sun, 01 Apr 2012 19:56:36 +0200
Den 18:51 1. april 2012 skrev Per Bull Holmen <email@hidden> følgende:
> Den 17:28 1. april 2012 skrev Nick <email@hidden> følgende:
>
>> However, I can't figure out how to make zooming like google maps does,
>> preserving that mouse "anchor" point's location. Could you give me a
>> hint?
>>
>> Thank you
>
> I can't give you readymade example code, but here's the steps:
OK, just because I am a naive altruist, I made one for you. This one
takes a zoom factor where 1 is 1:1, 0.5 means half size, 2.0 means
double size, etc. Works on my machine. I haven't checked what happens
if the bounds origin gets negative coordinates. You may want to put in
a little check to prevent that.
-(void)zoom:(float)newFactor event:(NSEvent *)mouseEvent {
NSScrollView *scrollView = [self enclosingScrollView];
NSClipView *clipView = [scrollView contentView];
NSRect clipViewBounds = [clipView bounds];
NSSize clipViewSize = [clipView frame].size;
NSPoint clipLocalPoint = [clipView convertPoint:[mouseEvent
locationInWindow] fromView:nil];
float xFraction = ( clipLocalPoint.x - clipViewBounds.origin.x ) /
clipViewBounds.size.width;
float yFraction = ( clipLocalPoint.y - clipViewBounds.origin.y ) /
clipViewBounds.size.height;
clipViewBounds.size.width = clipViewSize.width / newFactor;
clipViewBounds.size.height = clipViewSize.height / newFactor;
clipViewBounds.origin.x = clipLocalPoint.x - ( xFraction *
clipViewBounds.size.width );
clipViewBounds.origin.y = clipLocalPoint.y - ( yFraction *
clipViewBounds.size.height );
[clipView setBounds:clipViewBounds];
}
Per
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden