2010-07-28 2 views
9

Je voudrais qu'un UIView glisse du bas de l'écran (et reste au milieu de l'écran) comme une UIActionSheet. Comment puis-je accomplir cela?Comment puis-je présenter un UIView à partir du bas de l'écran comme une UIActionSheet?

MISE À JOUR: J'utilise le code suivant:

TestView* test = [[TestView alloc] initWithNibName:@"TestView" bundle:nil]; 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.4]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 

test.view.center = CGPointMake(160,100); 
//test.view.frame = CGRectMake(0, 0, 160, 210); 
[[[UIApplication sharedApplication] keyWindow] addSubview:test.view]; 

[UIView commitAnimations]; 

La vue semble être animant du coin et apparaissant dans le coin. Comment puis-je le faire glisser du bas? Se rapprocher!

+0

poste mis à jour avec un code qui devrait aider à – iwasrobbed

+0

un autre bon exemple: http://github.com/homeyer/CustomUIActionSheet –

Répondre

4

Faites ce que Matt a fait ici, mais changez simplement les valeurs et la direction. J'ai du code à la maison pour le faire à partir du bas si vous en avez besoin plus tard (je mettrai à jour cet article).

Lien: http://cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html

En outre, ne pas oublier de prendre le morceau de code qui déplace la vue principale vers le bas (donc plutôt le UIView saute juste au-dessus comme un ActionSheet)

Mise à jour avec Code:

C'est ce que je l'utilise dans un de mes applications pour afficher/cacher une petite vue « options »:

- (void)toggleOptions:(BOOL)ViewHidden 
{ 
// this method opens/closes the player options view (which sets repeat interval, repeat & delay on/off) 

if (ViewHidden == NO) 
{ 
    // delay and move view out of superview 
    CGRect optionsFrame = optionsController.view.frame; 

    [UIView beginAnimations:nil context:nil]; 

    optionsFrame.origin.y += optionsFrame.size.height; 
    optionsController.view.frame = optionsFrame; 

    [UIView commitAnimations]; 

    [optionsController.view 
    performSelector:@selector(removeFromSuperview) 
    withObject:nil 
    afterDelay:0.5]; 
    [optionsController 
    performSelector:@selector(release) 
    withObject:nil 
    afterDelay:0.5]; 
    optionsController = nil; 
} 
else 
{ 
    optionsController = [[PlayOptionsViewController alloc] init]; 

    // 
    // Position the options at bottom of screen 
    // 
    CGRect optionsFrame = optionsController.view.frame; 
    optionsFrame.origin.x = 0; 
    optionsFrame.size.width = 320; 
    optionsFrame.origin.y = 423; 

    // 
    // For the animation, move the view up by its own height. 
    // 
    optionsFrame.origin.y += optionsFrame.size.height; 

    optionsController.view.frame = optionsFrame; 
    [window addSubview:optionsController.view]; 

    [UIView beginAnimations:nil context:nil]; 

    optionsFrame.origin.y -= optionsFrame.size.height; 
    optionsController.view.frame = optionsFrame; 

    [UIView commitAnimations]; 
} 
} 
+0

J'aime votre solution. Est-il possible d'empêcher l'utilisateur d'interagir avec la vue derrière cette fenêtre contextuelle? –

+0

Un moyen simple serait de simplement désactiver l'interaction de l'utilisateur (c'est-à-dire '[someView setUserInteractionEnabled: NO];') de cette vue. Une meilleure façon serait de montrer d'abord une autre vue avec une sorte de dégradé semi-transparent avec l'interaction de l'utilisateur désactivée et * puis * d'animer votre nouvelle vue. De cette façon, il est logique pour l'utilisateur que, puisque l'autre vue est quelque peu obscurcie, ils ne peuvent plus interagir avec lui. Sinon, ils pourraient penser que quelque chose est cassé. Pensez simplement à la façon dont un UIAlertView le fait. Lorsque vous avez terminé, supprimez simplement les deux vues. – iwasrobbed

1

Vous devrez déplacer vous-même la vue en définissant center ou frame. Je vais vous laisser imaginer ce qu'il faut faire. Mais pour l'animation:

// set the view to its initial position here... 

[UIView beginAnimations:nil context:NULL]; 
// move the view into place here... 
[UIView commitAnimations]; 
+0

Merci pour le conseil! Je suis presque là. J'ai modifié mon message pour montrer mon code. Ce n'est pas exactement en train de glisser. –

4

Un moyen serait d'utiliser le présent contrôleur vue modal sur le contrôleur de vue:

presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated

Pour plus d'informations un coup d'oeil à la UIViewController documentation.

EDIT: Si vous voulez un affichage en plein écran, vous devez l'animer dans la position indiquée par @jtbandes. Je suggère également d'ajouter des bonbons au bloc d'animation UIView:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.4]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 

myView.center = CGPointMake(x,y); 

[UIView commitAnimations]; 

Vous pouvez ensuite le déplacer à nouveau si vous avez besoin d'aller en plein écran ou la rejeter.

+0

Je veux aussi qu'il se comporte comme une UIActionSheet, en affichant la vue en plein écran. –

+0

Merci pour le pourboire! Je suis presque là. J'ai modifié mon message pour montrer mon code. Ce n'est pas exactement en train de glisser. –

+0

Que diriez-vous d'une vue comme l'application gratuite ups?C'est comme une vue du centre de notification que l'utilisateur peut faire glisser/glisser et qui peut même réguler la hauteur de la vue par rapport à l'autre vue. – marciokoko

0

Découvrez ce post: http://blog.yetanotherjosh.com/post/33685102199/3-ways-to-do-a-vertical-transition-with

Je vais avec l'approche de la fenêtre modale.

+1

Notez que [les réponses aux liens sont déconseillées] (http://meta.stackoverflow.com/tags/link-only-answers/info), les réponses SO doivent être les suivantes: point final d'une recherche de solution (vs encore une autre escale de références, qui tendent à se périmer avec le temps). S'il vous plaît envisager d'ajouter un synopsis autonome ici, en gardant le lien comme référence. – kleopatra

0

Essayez cette solution .... cela fonctionne

#pragma mark - Date Selector View PresentModelView with Transparent ViewController 

- (void) showModal:(UIView*) modalView { 

    CGRect rect=modalView.frame; 
    rect.origin=CGPointMake(0, 0); 
    self.tutorialView.frame=rect; 

    UIWindow *mainWindow = [(AppDelegate *)[UIApplication sharedApplication].delegate window]; 

    CGPoint middleCenter; 


    middleCenter = CGPointMake(modalView.center.x, modalView.center.y); 

    CGSize offSize = [UIScreen mainScreen].bounds.size; 

    CGPoint offScreenCenter = CGPointMake(offSize.width/2.0, offSize.height * 1.5); 
    modalView.center = offScreenCenter; 

    if ([[mainWindow subviews] containsObject:modalView]) { 
     [modalView removeFromSuperview]; 
    } 

    [mainWindow addSubview:modalView]; 

    [mainWindow bringSubviewToFront:modalView]; 
    // Show it with a transition effect 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    // animation duration in seconds 
    modalView.center = middleCenter; 
    [UIView commitAnimations]; 

} 

// Use this to slide the semi-modal view back down. 
- (void) hideModal:(UIView*) modalView { 

    CGSize offSize = [UIScreen mainScreen].bounds.size; 
    CGPoint offScreenCenter = CGPointMake(offSize.width/2.0, offSize.height * 1.5); 
    [UIView beginAnimations:nil context:(__bridge void *)(modalView)]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(hideModalEnded:finished:context:)]; 
    modalView.center = offScreenCenter; 
    [UIView commitAnimations]; 

} 

- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

    UIView *modalView = (__bridge UIView *)context; 
    [modalView removeFromSuperview]; 
} 
+0

Supprimer l'animation ne fonctionne pas correctement –

Questions connexes