Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're just looking mimic the UIActionSheet, I'd just create a UIVew subclass called something like "CoolActionSheet" and programatically place the buttons on there. Then, when you press the buttons it triggers a delegate method in a protocol which will be implemented in your main view controller so do something.</p> <p>To show and hide the action picker use UIView animations <strong>in the CoolActionSheet class</strong> like so:</p> <pre><code>-(void)showSheet { NSLog(@"Showing sheet..."); //Set the x/y position of the action sheet to JUST off-screen CGFloat xPos = parentView.frame.origin.x; CGFloat yPos = parentView.frame.size.height+kActionSheetHeight; [self setFrame:CGRectMake(xPos, yPos, kActionSheetWidth, kActionSheetHeight)]; /*Here is where you would add your other UI objects such as buttons and set their @selector to a method in your CoolActionSheet protocol. You could then implement this delegate method in your main view controller to carry out a custom action. You might also want to add a background image to the view or something else. For example: */ UIButton *coolButton = [[UIButton alloc] initWithFrame:buttonDimensions]; [coolButton addTarget:self action:@selector(didDismissActionSheet) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:coolButton]; [self.parentView addSubview:self.view]; //Slide the sheet up from the bottom of the screen [UIView animateWithDuration:0.2 animations:^(void) { //Slide banner in from left to right [self setFrame:CGRectMake(0, yPos-kActionSheetHeight, kActionSheetWidth, kActionSheetHeight)]; }]; } </code></pre> <p>And to hide:</p> <pre><code>-(void)hideSheet { NSLog(@"Hiding"); CGFloat xPos = parentView.frame.origin.x; CGFloat yPos = parentView.frame.size.height+kActionSheetHeight; [UIView animateWithDuration:0.2 animations:^(void) { [self setFrame:CGRectMake(xPos, yPos, 320, 65)]; }completion:^(BOOL finished) { [self removeFromSuperview]; //Clean up }]; } </code></pre> <p>You may also want to grey-out the parent view. Again, in the CoolActionSheet.m:</p> <pre><code>-(void)shadeParentView { UIView *shadedView = [[UIView alloc] initWithFrame:CGRectMake(, 0, 320, 480)]; [shadedView addGestureRecognizer:gestureRecognizer]; [shadedView setBackgroundColor:[UIColor blackColor]]; [shadedView setAlpha:0.0]; [self addSubview:shadedView]; [UIView animateWithDuration:0.5 animations:^{ [shadedView setAlpha:0.5]; }]; </code></pre> <p>}</p> <p>You would need to set your <em>parentView</em> to the view controller's view. So, to call the action sheet in your main view controller you would:</p> <pre><code>CoolActionSheet *coolSheet = [[CoolActionSheet alloc] init]; [coolSheet setParentView:self.view]; [coolSheet setSheetDelegate:self]; //set the delegate to implement button press methods in this view controller </code></pre> <p>This might seem a bit long-winded, but it's a good MVC pattern to separate it out into another view class like this. And you now have a custom class that you can just import into any other project and it'll work!</p> <p>I haven't had a chance to test out this specific code, but the whole approach is good. Points to take away from this:</p> <ul> <li>Use a custom UIView class</li> <li>Implement delegate methods to execute tasks in your main view controller when a button is pressed in your subview.</li> <li>Implement a good MVC structure to avoid spaghetti code.</li> </ul> <p>Let me know if this helps :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload