Last active
August 29, 2015 13:56
-
-
Save keicoder/8910100 to your computer and use it in GitHub Desktop.
objective-c : snapBehavior example code
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
//snapBehavior example code | |
@implementation DynamicSandwichViewController | |
{ | |
NSMutableArray* _views; | |
UISnapBehavior* _snap; | |
BOOL _viewDocked; | |
} | |
- (void)someMethod | |
{ | |
[self tryDockView:someView]; //executes tryDockView: method | |
} | |
//선택된 뷰 이외 나머지 뷰의 알파값 변경 (투명 뷰로 만들기) | |
//show and hide the non-docked views so that the docked view occupies the entire screen | |
- (void)setAlphaWhenViewDocked:(UIView*)view alpha:(CGFloat)alpha { | |
for (UIView* aView in _views) { | |
if (aView != view) { | |
aView.alpha = alpha; | |
} | |
} | |
} | |
//checks whether the view has been dragged close to the top of the screen | |
//if it has, and the view is not yet docked | |
//creates a UISnapBehaviour which snaps the view to the center of the screen. | |
- (void)tryDockView:(UIView *)view { | |
BOOL viewHasReachedDockLocation = (view.frame.origin.y < 150.0); | |
if (viewHasReachedDockLocation) { | |
if (!_viewDocked) { | |
_snap = [[UISnapBehavior alloc] initWithItem:view snapToPoint:self.view.center]; | |
[_animator addBehavior:_snap]; | |
[self setAlphaWhenViewDocked:view alpha:0.0]; | |
_viewDocked = YES; | |
} | |
} else { | |
//If the view has been dragged away from the top of the screen and was previously docked | |
//removes the snap behavior (as a result view falls due to the gravity) | |
if (_viewDocked) { | |
[_animator removeBehavior:_snap]; | |
[UIView animateWithDuration:0.3 animations:^{ | |
[self setAlphaWhenViewDocked:view alpha:1.0]; | |
}]; | |
_viewDocked = NO; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment