Skip to content

Commit

Permalink
Tweaks to mixing in and out animation types
Browse files Browse the repository at this point in the history
  • Loading branch information
Collin Ruffenach committed Feb 16, 2014
1 parent f8819a8 commit 924e0a2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 52 deletions.
82 changes: 41 additions & 41 deletions CRToast/CRToast.m
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ - (CRToastAnimationType)inAnimationType {
kCRAnimationTypeDefaultIn;
}

- (CRToastAnimationType)ouAnimationType {
- (CRToastAnimationType)outAnimationType {
return _options[kCRToastAnimationOutTypeKey] ?
[_options[kCRToastAnimationOutTypeKey] integerValue] :
kCRAnimationTypeDefaultOut;
Expand Down Expand Up @@ -416,15 +416,15 @@ - (CGPoint)inCollisionPoint1 {
y = CGRectGetHeight(self.notificationViewAnimationFrame1)+kCRCollisionTweak;
break;
case CRToastAnimationDirectionLeft:
x = CGRectGetWidth(self.notificationViewAnimationFrame1)+2*kCRCollisionTweak;
x = CGRectGetWidth(self.notificationViewAnimationFrame1)+1.0;
y = CGRectGetHeight(self.notificationViewAnimationFrame1);
break;
case CRToastAnimationDirectionBottom:
x = CGRectGetWidth(self.notificationViewAnimationFrame1);
y = -kCRCollisionTweak;
break;
case CRToastAnimationDirectionRight:
x = -kCRCollisionTweak;
x = -kCRCollisionTweak-kCRCollisionTweak;
y = 0;
break;
}
Expand All @@ -440,15 +440,15 @@ - (CGPoint)inCollisionPoint2 {
y = CGRectGetHeight(self.notificationViewAnimationFrame1)+kCRCollisionTweak;
break;
case CRToastAnimationDirectionLeft:
x = CGRectGetWidth(self.notificationViewAnimationFrame1)+2*kCRCollisionTweak;
x = CGRectGetWidth(self.notificationViewAnimationFrame1)+1.0;
y = 0;
break;
case CRToastAnimationDirectionBottom:
x = 0;
y = -kCRCollisionTweak;
break;
case CRToastAnimationDirectionRight:
x = -kCRCollisionTweak;
x = -kCRCollisionTweak-kCRCollisionTweak;
y = CGRectGetHeight(self.notificationViewAnimationFrame1);
break;
}
Expand Down Expand Up @@ -653,7 +653,9 @@ - (void)displayNotification:(CRToast*)notification {
statusBarView.frame = notification.statusBarViewAnimationFrame1;
};

__weak __block typeof(self) blockSelf = self;
void (^outwardAnimationsBlock)(void) = ^void(void) {
[blockSelf.animator removeAllBehaviors];
notificationView.frame = notification.notificationViewAnimationFrame2;
statusBarView.frame = _notificationWindow.rootViewController.view.bounds;
};
Expand All @@ -675,38 +677,42 @@ - (void)displayNotification:(CRToast*)notification {
statusBarView.frame = notification.statusBarViewAnimationFrame2;
switch (notification.outAnimationType) {
case CRToastAnimationTypeLinear: {
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
options:0
animations:outwardAnimationsBlock
completion:outwardAnimationsCompletionBlock];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((notification.inAnimationType == CRToastAnimationTypeGravity ? notification.timeInterval : 0) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
options:0
animations:outwardAnimationsBlock
completion:outwardAnimationsCompletionBlock];
});
} break;
case CRToastAnimationTypeSpring: {
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
usingSpringWithDamping:notification.animationSpringDamping
initialSpringVelocity:notification.animationSpringInitialVelocity
options:0
animations:outwardAnimationsBlock
completion:outwardAnimationsCompletionBlock];

}
break;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((notification.inAnimationType == CRToastAnimationTypeGravity ? notification.timeInterval : 0) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:notification.animateOutTimeInterval
delay:notification.timeInterval
usingSpringWithDamping:notification.animationSpringDamping
initialSpringVelocity:notification.animationSpringInitialVelocity
options:0
animations:outwardAnimationsBlock
completion:outwardAnimationsCompletionBlock];
});
} break;
case CRToastAnimationTypeGravity: {
[_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
fromPoint:notification.outCollisionPoint1
toPoint:notification.outCollisionPoint2];
[_animator addBehavior:gravity];
[_animator addBehavior:collision];

self.gravityAnimationCompletionBlock = outwardAnimationsCompletionBlock;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(notification.timeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_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
fromPoint:notification.outCollisionPoint1
toPoint:notification.outCollisionPoint2];
[_animator addBehavior:gravity];
[_animator addBehavior:collision];

self.gravityAnimationCompletionBlock = outwardAnimationsCompletionBlock;
});
} break;
}
};

Expand Down Expand Up @@ -742,8 +748,6 @@ - (void)displayNotification:(CRToast*)notification {
self.gravityAnimationCompletionBlock = inwardAnimationsCompletionBlock;
}
break;
default:
break;
}
}

Expand All @@ -758,13 +762,9 @@ - (BOOL)showingNotification {;
- (void)collisionBehavior:(UICollisionBehavior*)behavior
endedContactForItem:(id <UIDynamicItem>)item
withBoundaryIdentifier:(id <NSCopying>)identifier {
CRToastView *notificationView = (CRToastView*)item;
if (self.gravityAnimationCompletionBlock) {
void (^gravityCompletionBlock)(BOOL) = [self.gravityAnimationCompletionBlock copy];
self.gravityAnimationCompletionBlock(YES);
self.gravityAnimationCompletionBlock = NULL;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(notificationView.toast.timeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
gravityCompletionBlock(YES);
});
}
}

Expand Down
13 changes: 8 additions & 5 deletions CRToastDemo/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ @interface MainViewController ()

@property (weak, nonatomic) IBOutlet UISegmentedControl *segFromDirection;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segToDirection;
@property (weak, nonatomic) IBOutlet UISegmentedControl *animationTypeSegmentedControl;
@property (weak, nonatomic) IBOutlet UISegmentedControl *inAnimationTypeSegmentedControl;
@property (weak, nonatomic) IBOutlet UISegmentedControl *outAnimationTypeSegmentedControl;

@property (weak, nonatomic) IBOutlet UISlider *sliderDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
Expand Down Expand Up @@ -59,8 +60,10 @@ - (void)viewDidLoad {
forState:UIControlStateNormal];
[self.segToDirection setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.animationTypeSegmentedControl setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.inAnimationTypeSegmentedControl setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.outAnimationTypeSegmentedControl setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[_scrollView addGestureRecognizer:tapGestureRecognizer];
Expand Down Expand Up @@ -120,8 +123,8 @@ - (NSDictionary*)options {
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastTextAlignmentKey : @(self.textAlignment),
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastAnimationInTypeKey : @(toastAnimationTypeFromSegmentedControl(_animationTypeSegmentedControl)),
kCRToastAnimationOutTypeKey : @(toastAnimationTypeFromSegmentedControl(_animationTypeSegmentedControl)),
kCRToastAnimationInTypeKey : @(toastAnimationTypeFromSegmentedControl(_inAnimationTypeSegmentedControl)),
kCRToastAnimationOutTypeKey : @(toastAnimationTypeFromSegmentedControl(_outAnimationTypeSegmentedControl)),
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex)} mutableCopy];
if (self.showImageSwitch.on) {
Expand Down
27 changes: 22 additions & 5 deletions CRToastDemo/MainViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MainViewController">
<connections>
<outlet property="animationTypeSegmentedControl" destination="woe-q8-hb2" id="aD7-oi-sxD"/>
<outlet property="contentView" destination="jNX-q4-3cu" id="d2u-Sj-LIA"/>
<outlet property="coverNavBarSwitch" destination="uOV-sj-WK6" id="Bas-cF-VRf"/>
<outlet property="inAnimationTypeSegmentedControl" destination="woe-q8-hb2" id="M5l-yA-965"/>
<outlet property="lblDuration" destination="oAK-DR-B27" id="roa-nx-2td"/>
<outlet property="outAnimationTypeSegmentedControl" destination="WGz-mj-dax" id="ZiZ-Lw-EG1"/>
<outlet property="scrollView" destination="csJ-tx-cXU" id="9Qm-4F-EkG"/>
<outlet property="segAlignment" destination="Db2-zb-fY5" id="S6H-bt-9A1"/>
<outlet property="segFromDirection" destination="vWx-wx-d32" id="uqN-Hk-EMi"/>
Expand All @@ -36,7 +37,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DId-6z-9ak">
<rect key="frame" x="102" y="416" width="121" height="62"/>
<rect key="frame" x="102" y="479" width="121" height="62"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Show Notification">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
Expand Down Expand Up @@ -65,7 +66,7 @@
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Notification Message" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="VqG-wR-oYI">
<rect key="frame" x="22" y="390" width="280" height="30"/>
<rect key="frame" x="22" y="453" width="280" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
Expand All @@ -81,7 +82,7 @@
</segments>
</segmentedControl>
<segmentedControl opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="Db2-zb-fY5">
<rect key="frame" x="22" y="354" width="280" height="29"/>
<rect key="frame" x="22" y="417" width="280" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Align Left"/>
Expand Down Expand Up @@ -141,13 +142,29 @@
<segment title="Gravity"/>
</segments>
</segmentedControl>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Animation Type" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="C9I-wl-POo">
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Entrance Animation Type" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="C9I-wl-POo">
<rect key="frame" x="0.0" y="281" width="320" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Thin" family="Helvetica Neue" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="WGz-mj-dax">
<rect key="frame" x="22" y="375" width="280" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Linear"/>
<segment title="Spring"/>
<segment title="Gravity"/>
</segments>
</segmentedControl>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Exit Animation Type" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yDz-5X-ME2">
<rect key="frame" x="0.0" y="346" width="320" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Thin" family="Helvetica Neue" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CRToast

`CRToast` is a library that allows you to easily create notifications that appear on the status bar. `CRToast` was originally based on [CWStatusBarNotification](https://github.com/cezarywojcik/CWStatusBarNotification) but the direction I found helpful wasn't something CWStatusBarNotification wanted to embrace.
`CRToast` is a library that allows you to easily create notifications that appear on top of or by pushing out the status bar or navigation bar. `CRToast` was originally based on [CWStatusBarNotification](https://github.com/cezarywojcik/CWStatusBarNotification).

![demo](screenshots/demo.gif)

Expand Down

0 comments on commit 924e0a2

Please sign in to comment.