Reputation: 7590
I'm programming a game using the new Sprite-Kit framework and want to use the mouse scroll wheel to change the player's gun. First of all i want to handle when the scroll wheel moves. I tried the following method, from the Cocoa Event Handling Guide
- (void)scrollWheel:(NSEvent *)theEvent
{
NSLog(@"SCROOOOOOOLL iN MOVeMENT");
}
Result: nothing, i didn't handle when i moved the mouse wheel.
Any idea of how can i handle this event ?
UPDATE: I saw two answers talking about class derivative from NSResponder or NSView but my class derives from SKScene, i'm programming using the Sprite-kit framework and obj-c doesn't allow multiple inheritance.
Here the class definition:
#import <SpriteKit/SpriteKit.h>
@interface OpcionesMenu : SKScene
@end
Upvotes: 4
Views: 5417
Reputation: 678
I think that could help:
- (void)scrollWheel:(NSEvent *)theEvent
{
[super scrollWheel:theEvent];
NSLog(@"user scrolled %f horizontally and %f vertically", theEvent.deltaX, theEvent.deltaY);
}
Upvotes: 3
Reputation: 6606
I know this is an old question, but I was having this exact problem with a collection view. So I have a NSScrollView
that contains a NSCollectionView
and some other views such as labels and buttons.
Anyway I wanted to get the collection view to be able to scroll horizontally and parent scroll view to to scroll vertically. I achieved this by storing and comparing the deltaX
and deltaY
values.
I made a custom scroll view class and set it to the collection view's scroll view.
Here is the header code:
#import <Cocoa/Cocoa.h>
@interface CustomCollectionScrollView : NSScrollView {
// Current scroll position.
CGFloat positionAxisX;
CGFloat positionAxisY;
}
@end
And here is the implementation code:
#import "CustomCollectionScrollView.h"
@implementation CustomCollectionScrollView
/// DRAW RECT METHOD ///
-(void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
positionAxisX = 0.0;
positionAxisY = 0.0;
}
/// SCROLL METHODS ///
-(void)scrollWheel:(NSEvent *)theEvent {
// Move the parent scroll view scroll view if the user is
// scrolling vertically otherwise move the collection scroll view.
if ((theEvent.deltaX == positionAxisX) && (theEvent.deltaY != positionAxisY)) {
[self.nextResponder scrollWheel:theEvent];
} else if ((theEvent.deltaX != positionAxisX) && (theEvent.deltaY == positionAxisY)) {
[super scrollWheel:theEvent];
}
// Update the current position values.
positionAxisX = theEvent.deltaX;
positionAxisY = theEvent.deltaY;
}
@end
Upvotes: 0
Reputation: 10355
Even though SKScene
derives from NSResponder
, it isn't a direct recipient of AppKit events. Rather, the SKView
(derived from NSView
/UIView
) that houses the scene handles the events. You can observe this by setting a breakpoint on your scene's mouseDown
method:
-[TWTMyScene mouseDown:]
-[SKView mouseDown:] ()
-[NSWindow sendEvent:] ()
-[NSApplication sendEvent:] ()
-[NSApplication run] ()
NSApplicationMain ()
So you can subclass SKView
and forward the scrollWheel
event yourself. Or, if you like living dangerously, you can just paste this category at the end of any of your code and it will take care of it for you:
@implementation SKView(ScrollTouchForwarding)
- (void)scrollWheel:(NSEvent *)event {
[self.scene scrollWheel:event];
}
@end
Upvotes: 6
Reputation: 22707
I don't know much about SpriteKit, but I see that there is an SKView
class that is a subclass of NSView
. I would guess that you should subclass SKView
and put your scrollWheel:
method there.
Upvotes: -1
Reputation: 53337
SKScene is part of SpriteKit which is an iOS framework, not an AppKit framework. Hence there is no scroll wheel handling at all (and no NSResponder (but UIResponder) in the inheritance chain). You can only work with touch events in the sprite kit.
Upvotes: -3