• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problems manually scrolling a view using mouseDown:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problems manually scrolling a view using mouseDown:


  • Subject: Re: Problems manually scrolling a view using mouseDown:
  • From: Matt Neuburg <email@hidden>
  • Date: Sun, 23 Jan 2005 21:15:19 -0800

On Fri, 21 Jan 2005 20:02:07 -0800, John Fox <email@hidden> said:

>I have an NSImageView subclass that is contained within an
>NSScrollView

Why? If you simply want to display an image in a scroll view, NSImageView is
way too much overhead. Just use an NSView that draws the image.

>I'm trying to make it possible to scroll the view by
>dragging the mouse in the view, but I've run into some problems.
>
>1) When dragging, my open hand cursor reverts to the arrow cursor.
>
>2) The scrolling doesn't seem to track evenly with the mouse

Your problem is caused by two things:

(1) You neglect to capture the original mouse down point in your mouseDown:
handler, so, as mouseDrag events start coming, you don't know how far to
scroll.

(2) You exit the event loop prematurely, instead of generating your own
event loop during the drag, as explained in the helpful document "Handling
Mouse Events in Views".

Just to give an example, here's a little code from an NSView where I allow
the user to scroll up/down (but not side to side) by dragging (clickPoint
and originalOrigin are ivars of the NSView):

- (void) mouseDown: (NSEvent*) e {
    NSScrollView* sv = [self enclosingScrollView];
    NSClipView* cv = [sv contentView];
    [[NSCursor openHandCursor] push];
    clickPoint = [e locationInWindow];
    originalOrigin = [cv bounds].origin;
    {
        BOOL keepOn = YES;
        while (keepOn) {
            NSEvent* theEvent = [[self window] nextEventMatchingMask:
NSLeftMouseUpMask | NSLeftMouseDraggedMask];
            NSPoint newPoint = [theEvent locationInWindow];
            NSPoint newOrigin;
            switch ([theEvent type]) {
                case NSLeftMouseDragged:
                    newOrigin = NSMakePoint(originalOrigin.x,
originalOrigin.y + (clickPoint.y - newPoint.y));
                    [cv scrollToPoint: [cv constrainScrollPoint:
newOrigin]];
                    [sv reflectScrolledClipView: cv];
                    originalOrigin = newOrigin;
                    clickPoint = newPoint;
                    break;
                case NSLeftMouseUp:
                    keepOn = NO;
                    [NSCursor pop];
                    break;
                default:
                    break;
            }
        }
    }
}


--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Problems manually scrolling a view using mouseDown:
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Manual binding questions.
  • Next by Date: Re: Problems manually scrolling a view using mouseDown:
  • Previous by thread: Problems manually scrolling a view using mouseDown:
  • Next by thread: Re: Problems manually scrolling a view using mouseDown:
  • Index(es):
    • Date
    • Thread