| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- //
- // PulldownMenu.m
- //
- // Created by Bernard Gatt
- //
- #import "PulldownMenu.h"
- @implementation PulldownMenu
- @synthesize menuList,
- handle,
- cellHeight,
- handleHeight,
- animationDuration,
- topMarginLandscape,
- topMarginPortrait,
- cellColor,
- cellFont,
- cellTextColor,
- cellSelectedColor,
- cellSelectionStyle,
- fullyOpen,
- delegate;
- - (id)init
- {
- self = [super init];
- self.backgroundColor = [UIColor clearColor];
- menuItems = [[NSMutableArray alloc] init];
-
- // Setting defaults
- cellHeight = 60.0f;
- handleHeight = 0.0f;
- animationDuration = 0.3f;
- topMarginPortrait = 0;
- topMarginLandscape = 0;
- cellColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];//[UIColor grayColor];
- cellSelectedColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.5];//[UIColor blackColor];
- cellFont = [UIFont fontWithName:@"GillSans-Bold" size:19.0f];
- cellTextColor = [UIColor whiteColor];
- cellSelectionStyle = UITableViewCellSelectionStyleDefault;
-
- return self;
- }
- - (id)initWithNavigationController:(UINavigationController *)navigationController
- {
- self = [self init];
-
- if (self)
- {
- [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
- [[NSNotificationCenter defaultCenter] addObserver: self
- selector: @selector(deviceOrientationDidChange:)
- name: UIDeviceOrientationDidChangeNotification
- object: nil];
-
- masterNavigationController = navigationController;
-
- navigationDragGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragMenu:)];
- navigationDragGestureRecognizer.minimumNumberOfTouches = 1;
- navigationDragGestureRecognizer.maximumNumberOfTouches = 1;
-
- handleDragGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragMenu:)];
- handleDragGestureRecognizer.minimumNumberOfTouches = 1;
- handleDragGestureRecognizer.maximumNumberOfTouches = 1;
-
- [masterNavigationController.navigationBar addGestureRecognizer:navigationDragGestureRecognizer];
-
- masterView = masterNavigationController.view;
- }
-
- return self;
- }
- - (id)initWithView:(UIView *)view
- {
- self = [self init];
-
- if (self)
- {
- topMargin = 0;
- masterView = view;
- }
-
- return self;
- }
- - (void)loadMenu
- {
- tableHeight = ([menuItems count] * cellHeight);
-
- [self updateValues];
-
- [self setFrame:CGRectMake(0, 0, 0, tableHeight+handleHeight)];
-
- fullyOpen = NO;
-
- menuList = [[UITableView alloc] init];
- menuList.scrollEnabled = false;
- menuList.backgroundColor = [UIColor clearColor];
- // self.backgroundColor = [UIColor yellowColor];
- // menuList.style
- [menuList setRowHeight:cellHeight];
- [menuList setDataSource:self];
- [menuList setDelegate:self];
- [self addSubview:menuList];
-
- handle = [[UIView alloc] init];
- [handle setBackgroundColor:[UIColor blackColor]];
-
- [self addSubview:handle];
-
- handleDragGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragMenu:)];
- handleDragGestureRecognizer.minimumNumberOfTouches = 1;
- handleDragGestureRecognizer.maximumNumberOfTouches = 1;
- [handle addGestureRecognizer:handleDragGestureRecognizer];
-
- [self setTranslatesAutoresizingMaskIntoConstraints:NO];
- [handle setTranslatesAutoresizingMaskIntoConstraints:NO];
- [menuList setTranslatesAutoresizingMaskIntoConstraints:NO];
-
- [self createConstraints];
- }
- - (void)insertButton:(NSString *)title
- {
- [menuItems addObject:title];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [self.delegate menuItemSelected:indexPath];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [menuItems count];
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuListCell"];
-
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuListCell"];
- }
-
- cell.backgroundColor = cellColor;
-
- // UIView *cellSelectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
- // cellSelectedBackgroundView.backgroundColor = cellSelectedColor;
- // cell.selectedBackgroundView = cellSelectedBackgroundView;
- cell.textLabel.textAlignment=NSTextAlignmentCenter;
- cell.selectionStyle = cellSelectionStyle;
-
- [cell.textLabel setTextColor:cellTextColor];
- cell.textLabel.font = cellFont;
- [cell.textLabel setText:[menuItems objectAtIndex:indexPath.item]];
-
- return cell;
- }
- - (void)dragMenu:(UIPanGestureRecognizer *)sender
- {
- if ([sender state] == UIGestureRecognizerStateChanged)
- {
- CGPoint gesturePosition = [sender translationInView:masterNavigationController.navigationBar];
- CGPoint newPosition = gesturePosition;
-
- newPosition.x = self.frame.size.width / 2;
-
- if (fullyOpen)
- {
- if (newPosition.y < 0)
- {
- newPosition.y += ((self.frame.size.height / 2) + topMargin);
-
- [self setCenter:newPosition];
- }
- }
- else
- {
- newPosition.y += -((self.frame.size.height / 2) - topMargin);
-
- if (newPosition.y <= ((self.frame.size.height / 2) + topMargin))
- {
- [self setCenter:newPosition];
- }
- }
- }
- else if ([sender state] == UIGestureRecognizerStateEnded)
- {
- [self animateDropDown];
- }
- }
- - (void)animateDropDown
- {
-
- [UIView animateWithDuration: animationDuration
- delay: 0.0
- options: UIViewAnimationOptionCurveEaseOut
- animations:^{
- if (self->fullyOpen)
- {
-
- self.center = CGPointMake(self.frame.size.width / 2, -((self.frame.size.height / 2) + self->topMargin));
- self->fullyOpen = NO;
- }
- else
- {
- self.center = CGPointMake(self.frame.size.width / 2, ((self.frame.size.height / 2) + self->topMargin));
- self->fullyOpen = YES;
- }
- }
- completion:^(BOOL finished){
- [self->delegate pullDownAnimated:self->fullyOpen];
- }];
- }
- - (void)animateDropDown:(CGPoint) position
- {
-
- [UIView animateWithDuration: animationDuration
- delay: 0.0
- options: UIViewAnimationOptionCurveEaseOut
- animations:^{
- if (self->fullyOpen)
- {
-
- self.center = CGPointMake(self.frame.size.width / 2+position.x, -((self.frame.size.height / 2)+position.y + self->topMargin));
- self->fullyOpen = NO;
- }
- else
- {
- self.center = CGPointMake(self.frame.size.width / 2+position.x, ((self.frame.size.height / 2)+position.y + self->topMargin));
- self->fullyOpen = YES;
- }
- }
- completion:^(BOOL finished){
- [self->delegate pullDownAnimated:self->fullyOpen];
- }];
- }
- - (void)createConstraints
- {
-
- NSLayoutConstraint *pullDownTopPositionConstraint = [NSLayoutConstraint
- constraintWithItem:self
- attribute:NSLayoutAttributeTop
- relatedBy:NSLayoutRelationEqual
- toItem:masterView
- attribute:NSLayoutAttributeTop
- multiplier:1.0
- constant:-self.frame.size.height];
-
- NSLayoutConstraint *pullDownCenterXPositionConstraint = [NSLayoutConstraint
- constraintWithItem:self
- attribute:NSLayoutAttributeCenterX
- relatedBy:NSLayoutRelationEqual
- toItem:masterView
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *pullDownWidthConstraint = [NSLayoutConstraint
- constraintWithItem:self
- attribute:NSLayoutAttributeWidth
- relatedBy:NSLayoutRelationEqual
- toItem:masterView
- attribute:NSLayoutAttributeWidth
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *pullDownHeightMaxConstraint = [NSLayoutConstraint
- constraintWithItem:self
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationLessThanOrEqual
- toItem:masterView
- attribute:NSLayoutAttributeHeight
- multiplier:0.5
- constant:0];
-
- pullDownHeightMaxConstraint.priority = 1000;
-
- NSLayoutConstraint *pullDownHeightConstraint = [NSLayoutConstraint
- constraintWithItem:self
- attribute:NSLayoutAttributeHeight
- relatedBy:0
- toItem:nil
- attribute:NSLayoutAttributeNotAnAttribute
- multiplier:1.0
- constant:tableHeight+handleHeight];
-
- pullDownHeightConstraint.priority = 900;
-
- NSLayoutConstraint *pullHandleWidthConstraint = [NSLayoutConstraint
- constraintWithItem:handle
- attribute:NSLayoutAttributeWidth
- relatedBy:NSLayoutRelationEqual
- toItem:masterView
- attribute:NSLayoutAttributeWidth
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *pullHandleHeightConstraint = [NSLayoutConstraint
- constraintWithItem:handle
- attribute:NSLayoutAttributeHeight
- relatedBy:0
- toItem:nil
- attribute:NSLayoutAttributeNotAnAttribute
- multiplier:1.0
- constant:handleHeight];
-
- NSLayoutConstraint *pullHandleBottomPositionConstraint = [NSLayoutConstraint
- constraintWithItem:handle
- attribute:NSLayoutAttributeBottom
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeBottom
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *pullHandleCenterPositionConstraint = [NSLayoutConstraint
- constraintWithItem:handle
- attribute:NSLayoutAttributeCenterX
- relatedBy:0
- toItem:self
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *menuListHeightMaxConstraint = [NSLayoutConstraint
- constraintWithItem:menuList
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationLessThanOrEqual
- toItem:masterView
- attribute:NSLayoutAttributeHeight
- multiplier:1.0
- constant:-topMargin];
-
- NSLayoutConstraint *menuListHeightConstraint = [NSLayoutConstraint
- constraintWithItem:menuList
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeHeight
- multiplier:1.0
- constant:-handleHeight];
-
- NSLayoutConstraint *menuListWidthConstraint = [NSLayoutConstraint
- constraintWithItem:menuList
- attribute:NSLayoutAttributeWidth
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeWidth
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *menuListCenterXPositionConstraint = [NSLayoutConstraint
- constraintWithItem:menuList
- attribute:NSLayoutAttributeCenterX
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0
- constant:0];
-
- NSLayoutConstraint *menuListTopPositionConstraint = [NSLayoutConstraint
- constraintWithItem:menuList
- attribute:NSLayoutAttributeTop
- relatedBy:NSLayoutRelationEqual
- toItem:self
- attribute:NSLayoutAttributeTop
- multiplier:1.0
- constant:0];
-
- [masterView addConstraint: pullDownTopPositionConstraint];
- [masterView addConstraint: pullDownCenterXPositionConstraint];
- [masterView addConstraint: pullDownWidthConstraint];
- [masterView addConstraint: pullDownHeightConstraint];
- [masterView addConstraint: pullDownHeightMaxConstraint];
-
- [masterView addConstraint: pullHandleHeightConstraint];
- [masterView addConstraint: pullHandleWidthConstraint];
- [masterView addConstraint: pullHandleBottomPositionConstraint];
- [masterView addConstraint: pullHandleCenterPositionConstraint];
-
- [masterView addConstraint: menuListHeightMaxConstraint];
- [masterView addConstraint: menuListHeightConstraint];
- [masterView addConstraint: menuListWidthConstraint];
- [masterView addConstraint: menuListCenterXPositionConstraint];
- [masterView addConstraint: menuListTopPositionConstraint];
-
- }
- - (void)deviceOrientationDidChange:(NSNotification *)notification
- {
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
-
- if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation) {
- return;
- }
-
- currentOrientation = orientation;
-
- [self performSelector:@selector(orientationChanged) withObject:nil afterDelay:0];
- }
- - (void)orientationChanged
- {
- [self updateValues];
-
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
- if ((UIDeviceOrientationIsPortrait(currentOrientation) && UIDeviceOrientationIsPortrait(orientation)) ||
- (UIDeviceOrientationIsLandscape(currentOrientation) && UIDeviceOrientationIsLandscape(orientation))) {
-
- currentOrientation = orientation;
-
- if (fullyOpen)
- {
- [self animateDropDown];
- }
-
- return;
- }
- }
- - (void)updateValues
- {
- topMargin = 0;
-
- BOOL isStatusBarShowing = ![[UIApplication sharedApplication] isStatusBarHidden];
-
- if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation]/*self.window.rootViewController.interfaceOrientation*/)) {
- if (isStatusBarShowing) { topMargin = [UIApplication.sharedApplication statusBarFrame].size.width; }
- topMargin += topMarginLandscape;
- }
- else
- {
- if (isStatusBarShowing) { topMargin = [UIApplication.sharedApplication statusBarFrame].size.height; }
- topMargin += topMarginPortrait;
- }
-
- if (masterNavigationController != nil)
- {
- topMargin += masterNavigationController.navigationBar.frame.size.height;
- }
- }
- @end
|