Skip to content

Commit

Permalink
Add support for mouse scrolling horizontally from left or right (And …
Browse files Browse the repository at this point in the history
…ARC now)
  • Loading branch information
tualatrix committed Jul 15, 2013
1 parent bda5960 commit b8bd661
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Classes/MIHSliderView.h
Expand Up @@ -34,7 +34,8 @@
typedef enum {
MIHSliderTransitionFade,
MIHSliderTransitionPushVertical,
MIHSliderTransitionPushHorizontal
MIHSliderTransitionPushHorizontalFromLeft,
MIHSliderTransitionPushHorizontalFromRight
} MIHSliderTransition;

@class MIHSliderDotsControl;
Expand Down
72 changes: 46 additions & 26 deletions Classes/MIHSliderView.m
Expand Up @@ -30,6 +30,8 @@ @interface MIHSliderView () // private API
@property (retain) NSView *displayedSlide;
@property (retain) NSTimer *transitionTimer;
@property (retain) NSView *contentView;
@property (assign) CGFloat scrollDeltaX;
@property (assign) CGFloat scrollDeltaY;
- (void)_prepareView;
- (void)_prepareTransitionTimer;
- (void)_prepareTransitionToIndex:(NSUInteger)index;
Expand All @@ -48,16 +50,6 @@ @implementation MIHSliderView {
NSTimeInterval _scheduledTransitionInterval;
}

- (void)dealloc
{
[_contentView release];
[_dotsControl release];
[_slides release];
[_displayedSlide release];
[_transitionTimer release];
[super dealloc];
}

- (id)init
{
self = [super init];
Expand Down Expand Up @@ -148,7 +140,7 @@ - (void)displaySlideAtIndex:(NSUInteger)aIndex
if (slideToDisplay == self.displayedSlide) return;
slideToDisplay.frame = self.bounds;
[slideToDisplay setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

if (self.displayedSlide == nil) {
[self.contentView addSubview:slideToDisplay];
self.displayedSlide = slideToDisplay;
Expand Down Expand Up @@ -193,11 +185,39 @@ - (NSTimeInterval)scheduledTransitionInterval
return _scheduledTransitionInterval;
}

- (void)scrollWheel:(NSEvent *)theEvent {
if (theEvent.phase == NSEventPhaseBegan) {
self.scrollDeltaX = 0;
self.scrollDeltaY = 0;
} else if (theEvent.phase == NSEventPhaseChanged) {
self.scrollDeltaX += theEvent.scrollingDeltaX;
self.scrollDeltaY += theEvent.scrollingDeltaY;
} else if (theEvent.phase == NSEventPhaseEnded) {
if (self.scrollDeltaX > 50) {
self.transitionStyle = MIHSliderTransitionPushHorizontalFromLeft;
[self displaySlideAtIndex:(self.indexOfDisplayedSlide - 1) % self.slides.count];
if (self.scheduledTransition) {
[self _prepareTransitionTimer];
}
} else if (self.scrollDeltaX < - 50) {
self.transitionStyle = MIHSliderTransitionPushHorizontalFromRight;
[self displaySlideAtIndex:(self.indexOfDisplayedSlide + 1) % self.slides.count];
if (self.scheduledTransition) {
[self _prepareTransitionTimer];
}
}
} else if (theEvent.phase == NSEventPhaseCancelled) {
self.scrollDeltaX = 0;
self.scrollDeltaY = 0;
}
}

#pragma mark -

- (void)_prepareView
{
self.contentView = [[[NSView alloc] initWithFrame:self.bounds] autorelease];
[self setAcceptsTouchEvents:YES];
self.contentView = [[NSView alloc] initWithFrame:self.bounds];
self.contentView.wantsLayer = YES;
[self addSubview:self.contentView];
_dotsControl = [[MIHSliderDotsControl alloc] init];
Expand All @@ -222,13 +242,17 @@ - (void)_dotViewSelected:(NSView *)dotView
[self _prepareTransitionTimer]; // <- this will restart the timer
}
if (dotView.tag >= 0 && dotView.tag < self.slides.count) {
NSLog(@"dotView, %ld crrut View:%ld", dotView.tag, self.dotsControl.indexOfHighlightedDot);

self.transitionStyle = dotView.tag > self.dotsControl.indexOfHighlightedDot ? MIHSliderTransitionPushHorizontalFromRight: MIHSliderTransitionPushHorizontalFromLeft;

[self displaySlideAtIndex:dotView.tag];
}
}

- (void)_prepareTransitionToIndex:(NSUInteger)aIndex
{
const CFTimeInterval duration = self.transitionAnimationDuration * ([self.window currentEvent].modifierFlags & NSShiftKeyMask) ? 4.0 : 1.0;
const CFTimeInterval duration = self.transitionAnimationDuration * ([self.window currentEvent].modifierFlags & NSShiftKeyMask) ? 1.5 : 0.3;
CATransition *transition = [CATransition animation];
[transition setDuration:duration];
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
Expand All @@ -237,12 +261,16 @@ - (void)_prepareTransitionToIndex:(NSUInteger)aIndex
[transition setType:kCATransitionFade];
break;

case MIHSliderTransitionPushHorizontal:
[transition setType:kCATransitionMoveIn];
case MIHSliderTransitionPushHorizontalFromLeft:
[transition setType:kCATransitionPush];
//[transition setSubtype:(self.indexOfDisplayedSlide < aIndex ? kCATransitionFromRight : kCATransitionFromLeft)];
[transition setSubtype:kCATransitionFromLeft];
break;

case MIHSliderTransitionPushHorizontalFromRight:
[transition setType:kCATransitionPush];
//[transition setSubtype:(self.indexOfDisplayedSlide < aIndex ? kCATransitionFromRight : kCATransitionFromLeft)];
[transition setSubtype:kCATransitionFromRight];
break;
case MIHSliderTransitionPushVertical:
[transition setType:kCATransitionMoveIn];
//[transition setSubtype:(self.indexOfDisplayedSlide < aIndex ? kCATransitionFromTop : kCATransitionFromBottom)];
Expand Down Expand Up @@ -274,8 +302,8 @@ - (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
_normalDotImage = [[aDecoder decodeObjectForKey:@"_normalDotImage"] retain];
_highlightedDotImage = [[aDecoder decodeObjectForKey:@"_highlightedDotImage"] retain];
_normalDotImage = [aDecoder decodeObjectForKey:@"_normalDotImage"];
_highlightedDotImage = [aDecoder decodeObjectForKey:@"_highlightedDotImage"];
}
return self;
}
Expand All @@ -287,13 +315,6 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[super encodeWithCoder:aCoder];
}

- (void)dealloc
{
[_normalDotImage release];
[_highlightedDotImage release];
[super dealloc];
}

- (void)setDotsCount:(NSUInteger)dotsCount
{
_dotsCount = dotsCount;
Expand All @@ -314,7 +335,6 @@ - (void)setDotsCount:(NSUInteger)dotsCount
[dotView setAction:@selector(_dotViewSelected:)];

[self addSubview:dotView];
[dotView release];
}

self.frame = NSMakeRect((self.sliderView.bounds.size.width - (dotsCount * kSpaceBetweenDotCenters + kDotImageSize) + kSpaceBetweenDotCenters) / 2, kDotContainerY, dotsCount * kSpaceBetweenDotCenters + kDotImageSize, kDotImageSize);
Expand Down

0 comments on commit b8bd661

Please sign in to comment.