如何像UIActionSheet一样从屏幕底部呈现UIView?

时间:2010-07-28 20:07:21

标签: iphone objective-c cocoa-touch uiview core-animation

我希望UIView从屏幕底部向上滑动(并保持在屏幕中间),就像UIActionSheet一样。我怎么能做到这一点?

更新 我使用以下代码:

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];  

视图似乎是从角落动画并出现在角落里。如何让它从底部向上滑动?越来越近了!

5 个答案:

答案 0 :(得分:4)

一种方法是在视图控制器上使用当前的模态视图控制器:

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

有关详细信息,请查看UIViewController documentation

编辑:如果你想要一个中间屏幕视图,你需要将它设置为@jtbandes指出的位置。我建议还为UIView动画块添加一些糖果:

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

myView.center = CGPointMake(x,y);

[UIView commitAnimations];

如果您需要全屏或关闭它,您可以再次移动它。

答案 1 :(得分:4)

做Matt在这里做的事,但只是改变价值观和方向。如果您以后需要,我在家里有代码从底部执行此操作(我将更新此帖子)。

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

另外,不要忘记取出向下移动主视图的代码(因此UIView只是像ActionSheet一样弹出顶部)

更新了代码:

我在我的一个应用程序中使用这些来显示/隐藏一些“选项”视图:

- (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];
}
}

答案 2 :(得分:1)

您必须自行移动视图,方法是设置centerframe。我会让你弄清楚要设置的内容。但对于动画:

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

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

答案 3 :(得分:0)

答案 4 :(得分:0)

试试这个解决方案....它有效

#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];
}