Created
June 25, 2014 18:58
-
-
Save andkon/a49837e57a835f3fa34d to your computer and use it in GitHub Desktop.
Notification views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface RootVC : UIViewController <UIDynamicAnimatorDelegate> | |
| @property (strong, nonatomic) UIDynamicAnimator *animator; | |
| @property (strong, nonatomic) UIView *noticeView; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; | |
| self.animator.delegate = self; | |
| } | |
| - (void)saveArticle:(id)sender | |
| { | |
| // initialize then add the noticeView | |
| self.noticeView = [[UIView alloc] init]; | |
| // set the noticeView.frame off screen | |
| self.noticeView.frame = CGRectMake(0, -20, 320, 30); | |
| self.noticeView.noticeLabel.text = @"Saved!"; | |
| [self.scrollView addSubview:self.noticeView]; | |
| // add a snap behavior - the snap point being the centre of where you want the view to be. | |
| UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:self.noticeView snapToPoint:CGPointMake(160, 15)]; | |
| // make it look sexy | |
| snapBehavior.damping = 0.3; | |
| [self.animator addBehavior:snapBehavior]; | |
| } | |
| // called when the noticeView comes to rest: | |
| // pushes off screen to finish | |
| - (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator | |
| { | |
| [self.animator removeAllBehaviors]; | |
| UIPushBehavior *removePush = [[UIPushBehavior alloc] initWithItems:@[self.noticeView] mode:UIPushBehaviorModeInstantaneous]; | |
| removePush.pushDirection = CGVectorMake(9,0); | |
| [self.animator addBehavior:pushBehavior]; | |
| removePush.action = ^{ | |
| if(!CGRectIntersectsRect(self.view.frame, self.noticeView.frame)) { | |
| [self.animator removeBehavior:pushBehavior]; | |
| [self.noticeView removeFromSuperView]; | |
| } | |
| } | |
| } | |
| // or the cooler version, falling down with a bit of an angle: | |
| - (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator | |
| { | |
| if([self.noticeView isDescendantOfView:self.view]) { | |
| [self.noticeView removeFromSuperview]; | |
| [self.animator removeAllBehaviors]; | |
| } | |
| [self.animator removeAllBehaviors]; | |
| UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc[ initWithItem:self.noticeView]; | |
| gravityBehavior.gravityDirection = CGVectorMake(0,5); | |
| [self.animator addBehavior:gravityBehavior]; | |
| UIDynamicItemBehavior *dynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.noticeView]]; | |
| [dynamicBehavior addAngularVelocity:(M_PI / 2) forItem:self.noticeView]; | |
| [self.animator addBehavior:dynamicBehavior]; | |
| gravityBehavior.action = ^{ | |
| if(!CGRectIntersectsRect(self.view.frame, self.noticeView.frame)) { | |
| [self.animator removeBehavior:gravityBehavior]; | |
| [self.noticeView removeFromSuperView]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment