Skip to content

Commit

Permalink
Support for setting gravity animation magnitude
Browse files Browse the repository at this point in the history
  • Loading branch information
Collin Ruffenach committed Feb 16, 2014
1 parent bc4a8e1 commit f8819a8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CRToast/CRToast.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extern NSString *const kCRToastAnimationOutTimeIntervalKey;
extern NSString *const kCRToastAnimationSpringDampingKey;
extern NSString *const kCRToastAnimationSpringInitialVelocityKey;

extern NSString *const kCRToastAnimationGravityMagnitudeKey;

extern NSString *const kCRToastTextKey;
extern NSString *const kCRToastFontKey;
extern NSString *const kCRToastTextColorKey;
Expand Down
29 changes: 20 additions & 9 deletions CRToast/CRToast.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ @interface CRToast : NSObject
@property (nonatomic, readonly) NSTimeInterval animateOutTimeInterval;

@property (nonatomic, readonly) CGFloat animationSpringDamping;
@property (nonatomic, readonly) CGFloat animationInitialVelocity;
@property (nonatomic, readonly) CGFloat animationSpringInitialVelocity;
@property (nonatomic, readonly) CGFloat animationGravityMagnitude;

@property (nonatomic, readonly) NSString *text;
@property (nonatomic, readonly) UIFont *font;
Expand Down Expand Up @@ -85,6 +86,7 @@ @interface CRToastView : UIView

NSString *const kCRToastAnimationSpringDampingKey = @"kCRToastAnimationSpringDampingKey";
NSString *const kCRToastAnimationSpringInitialVelocityKey = @"kCRToastAnimateSpringVelocityKey";
NSString *const kCRToastAnimationGravityMagnitudeKey = @"kCRToastAnimationGravityMagnitudeKey";

NSString *const kCRToastTextKey = @"kCRToastTextKey";
NSString *const kCRToastFontKey = @"kCRToastFontKey";
Expand Down Expand Up @@ -112,6 +114,7 @@ @interface CRToastView : UIView

static CGFloat kCRSpringDampingDefault = 0.6;
static CGFloat kCRSpringInitialVelocityDefault = 1.0;
static CGFloat kCRGravityMagnitudeDefault = 1.0;

static NSString * kCRTextDefault = @"";
static UIFont * kCRFontDefault = nil;
Expand Down Expand Up @@ -209,15 +212,16 @@ + (void)setDefaultOptions:(NSDictionary*)defaultOptions {

if (defaultOptions[kCRToastAnimationInTypeKey]) kCRAnimationTypeDefaultIn = [defaultOptions[kCRToastAnimationInTypeKey] integerValue];
if (defaultOptions[kCRToastAnimationOutTypeKey]) kCRAnimationTypeDefaultOut = [defaultOptions[kCRToastAnimationOutTypeKey] integerValue];
if (defaultOptions[kCRToastAnimationInDirectionKey]) kCRInAnimationDirectionDefault = [defaultOptions[kCRToastAnimationInDirectionKey] integerValue];
if (defaultOptions[kCRToastAnimationOutDirectionKey]) kCROutAnimationDirectionDefault = [defaultOptions[kCRToastAnimationOutDirectionKey] integerValue];
if (defaultOptions[kCRToastAnimationInDirectionKey]) kCRInAnimationDirectionDefault = [defaultOptions[kCRToastAnimationInDirectionKey] integerValue];
if (defaultOptions[kCRToastAnimationOutDirectionKey]) kCROutAnimationDirectionDefault = [defaultOptions[kCRToastAnimationOutDirectionKey] integerValue];

if (defaultOptions[kCRToastAnimationInTimeIntervalKey]) kCRAnimateInTimeIntervalDefault = [defaultOptions[kCRToastAnimationInTimeIntervalKey] doubleValue];
if (defaultOptions[kCRToastTimeIntervalKey]) kCRTimeIntervalDefault = [defaultOptions[kCRToastTimeIntervalKey] doubleValue];
if (defaultOptions[kCRToastAnimationOutTimeIntervalKey]) kCRAnimateOutTimeIntervalDefault = [defaultOptions[kCRToastAnimationOutTimeIntervalKey] doubleValue];

if (defaultOptions[kCRToastAnimationSpringDampingKey]) kCRSpringDampingDefault = [defaultOptions[kCRToastAnimationSpringDampingKey] floatValue];
if (defaultOptions[kCRToastAnimationSpringInitialVelocityKey]) kCRSpringInitialVelocityDefault = [defaultOptions[kCRToastAnimationSpringInitialVelocityKey] floatValue];
if (defaultOptions[kCRToastAnimationGravityMagnitudeKey]) kCRGravityMagnitudeDefault = [defaultOptions[kCRToastAnimationGravityMagnitudeKey] floatValue];

if (defaultOptions[kCRToastTextKey]) kCRTextDefault = defaultOptions[kCRToastTextKey];
if (defaultOptions[kCRToastFontKey]) kCRFontDefault = defaultOptions[kCRToastFontKey];
Expand Down Expand Up @@ -319,7 +323,7 @@ - (NSTimeInterval)animateOutTimeInterval {
kCRAnimateOutTimeIntervalDefault;
}

- (CGFloat)animationInitialVelocity {
- (CGFloat)animationSpringInitialVelocity {
return _options[kCRToastAnimationSpringInitialVelocityKey] ?
[_options[kCRToastAnimationSpringInitialVelocityKey] floatValue] :
kCRSpringInitialVelocityDefault;
Expand All @@ -331,6 +335,12 @@ - (CGFloat)animationSpringDamping {
kCRSpringDampingDefault;
}

- (CGFloat)animationGravityMagnitude {
return _options[kCRToastAnimationGravityMagnitudeKey] ?
[_options[kCRToastAnimationGravityMagnitudeKey] floatValue] :
kCRGravityMagnitudeDefault;
}

- (NSString*)text {
return _options[kCRToastTextKey] ?: kCRTextDefault;
}
Expand Down Expand Up @@ -663,7 +673,7 @@ - (void)displayNotification:(CRToast*)notification {

void (^inwardAnimationsCompletionBlock)(BOOL) = ^void(BOOL finished) {
statusBarView.frame = notification.statusBarViewAnimationFrame2;
switch (notification.inAnimationType) {
switch (notification.outAnimationType) {
case CRToastAnimationTypeLinear: {
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
Expand All @@ -675,7 +685,7 @@ - (void)displayNotification:(CRToast*)notification {
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
usingSpringWithDamping:notification.animationSpringDamping
initialSpringVelocity:notification.animationInitialVelocity
initialSpringVelocity:notification.animationSpringInitialVelocity
options:0
animations:outwardAnimationsBlock
completion:outwardAnimationsCompletionBlock];
Expand All @@ -686,6 +696,7 @@ - (void)displayNotification:(CRToast*)notification {
[_animator removeAllBehaviors];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[notificationView]]; ;
gravity.gravityDirection = notification.outGravityDirection;
gravity.magnitude = notification.animationGravityMagnitude;
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[notificationView]];
collision.collisionDelegate = self;
[collision addBoundaryWithIdentifier:kCRToastManagerCollisionBoundryIdentifier
Expand All @@ -710,7 +721,7 @@ - (void)displayNotification:(CRToast*)notification {
[UIView animateWithDuration:notification.animateInTimeInterval
delay:0.0
usingSpringWithDamping:notification.animationSpringDamping
initialSpringVelocity:notification.animationInitialVelocity
initialSpringVelocity:notification.animationSpringInitialVelocity
options:0
animations:inwardAnimationsBlock
completion:inwardAnimationsCompletionBlock];
Expand All @@ -720,14 +731,14 @@ - (void)displayNotification:(CRToast*)notification {
[_animator removeAllBehaviors];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[notificationView]];
gravity.gravityDirection = notification.inGravityDirection;
gravity.magnitude = notification.animationGravityMagnitude;
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[notificationView]];
collision.collisionDelegate = self;
[collision addBoundaryWithIdentifier:kCRToastManagerCollisionBoundryIdentifier
fromPoint:notification.inCollisionPoint1
toPoint:notification.inCollisionPoint2];
[_animator addBehavior:gravity];
[_animator addBehavior:collision];

self.gravityAnimationCompletionBlock = inwardAnimationsCompletionBlock;
}
break;
Expand All @@ -747,7 +758,6 @@ - (BOOL)showingNotification {;
- (void)collisionBehavior:(UICollisionBehavior*)behavior
endedContactForItem:(id <UIDynamicItem>)item
withBoundaryIdentifier:(id <NSCopying>)identifier {
NSLog(@"contact");
CRToastView *notificationView = (CRToastView*)item;
if (self.gravityAnimationCompletionBlock) {
void (^gravityCompletionBlock)(BOOL) = [self.gravityAnimationCompletionBlock copy];
Expand All @@ -757,4 +767,5 @@ - (void)collisionBehavior:(UICollisionBehavior*)behavior
});
}
}

@end
4 changes: 2 additions & 2 deletions CRToastDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ - (NSDictionary*)options {
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastAnimationInTypeKey : @(toastAnimationTypeFromSegmentedControl(_animationTypeSegmentedControl)),
kCRToastAnimationOutTypeKey : @(toastAnimationTypeFromSegmentedControl(_animationTypeSegmentedControl)),
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex)} mutableCopy];
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex)} mutableCopy];
if (self.showImageSwitch.on) {
options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon.png"];
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Generates this
`CRToast` also allows for animation customization. This includes.
- Animation Type (Linear or Spring)
- Animation Type (Linear, Spring or Gravity)
- Animation Physics Coefficients (Spring Damping, Spring Initial Velocity, Gravity Magnitude)
- Presentation Type (Slide over bars or push content out)
- Direction (Enter and exit in any direction)
- Enter, Stay on Screen and Exit Timing
Expand Down

0 comments on commit f8819a8

Please sign in to comment.