【问题标题】:How can I present a UIView from the bottom of the screen like a UIActionSheet?如何像 UIActionSheet 一样从屏幕底部呈现 UIView?
【发布时间】:2010-07-28 20:07:21
【问题描述】:

我希望 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];  

视图似乎是从角落动画并出现在角落里。我怎样才能让它从底部向上滑动?接近了!

【问题讨论】:

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


【解决方案1】:

做马特在这里所做的,但只是改变价值观和方向。如果您以后需要,我在家里有代码可以从底部执行此操作(我会更新这篇文章)。

链接: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];
}
}

【讨论】:

  • 我喜欢你的解决方案。有什么方法可以阻止用户与此弹出窗口后面的视图进行交互?
  • 一种简单的方法是禁用该视图的用户交互(即[someView setUserInteractionEnabled: NO];)。更好的方法是首先显示另一个具有某种半透明渐变的视图,并禁用用户交互,然后然后为您的新视图设置动画。这种方式对用户来说是有意义的,因为另一个视图有点变暗,他们不能再与之交互。否则他们可能会认为有些东西坏了。想想 UIAlertView 是如何做到这一点的。完成后,只需删除两个视图
【解决方案2】:

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

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

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

【讨论】:

  • 我希望它的行为也像 UIActionSheet,通过在屏幕中间显示视图。
  • 感谢您的提示!我快到了。我修改了我的帖子以显示我的代码。它并没有完全向上滑动。
  • 免费 UPS 应用之类的视图怎么样?它就像一个通知中心视图,用户可以拖动/滑动,甚至可以调节视图在另一个视图上向上或向下显示的距离。
【解决方案3】:

您必须自己移动视图,方法是设置其centerframe。我会让你弄清楚将它们设置为什么。但是对于动画:

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

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

【讨论】:

  • 感谢您的提示!我快到了。我修改了我的帖子以显示我的代码。它并没有完全向上滑动。
【解决方案4】:

【讨论】:

  • 请注意,link-only answers are discouraged,SO 答案应该是搜索解决方案的终点(相对于另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
【解决方案5】:

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

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

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 2011-11-08
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多