2010-07-28 67 views
9

我想讓UIView從屏幕底部向上滑動(並保持在屏幕中間),就像UIActionSheet一樣。我怎樣才能做到這一點?如何從UIActionSheet的屏幕底部顯示UIView?

UPDATE: 我使用下面的代碼:

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

的觀點似乎從角落和動畫出現在角落裏。我怎樣才能讓它從底部向上滑動?越來越接近!

+0

更新後一些代碼,應該幫助 – iwasrobbed 2010-07-28 22:55:20

+0

另一個很好的例子:http://github.com/homeyer/CustomUIActionSheet – 2010-07-29 04:19:33

回答

4

做馬特在這裏做了什麼,但只是改變了價值和方向。如果您以後需要它,我有家庭代碼可以從底部執行此操作(我將更新此帖)。

鏈接: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]; 
} 
} 
+0

我喜歡你的解決方案。有什麼辦法可以阻止用戶與彈出窗口後面的視圖進行交互? – 2010-07-29 03:22:32

+0

一種簡單的方法是禁用該視圖的用戶交互(即'[someView setUserInteractionEnabled:NO];')。更好的方法是首先顯示另一個視圖,其中禁用用戶交互的某種半透明漸變,然後爲您的新視圖設置動畫效果。這樣對用戶來說是有意義的,因爲其他視圖有些暗淡,他們不能再與它交互了。否則,他們可能會認爲某些事情已經破裂試想一下UIAlertView如何做到這一點。當你完成它,只需刪除兩個視圖 – iwasrobbed 2010-07-29 03:43:08

1

您必須自行移動視圖,方法是設置其centerframe。我會讓你弄清楚該怎麼做。但對於動畫:

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

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

謝謝你的提示!我快到了。我修改了我的帖子以顯示我的代碼。它並不完全滑動。 – 2010-07-29 02:59:21

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

然後,您可以再次移動它,如果你需要去全屏或駁回。

+0

我希望它的行爲像UIActionSheet一樣,通過在屏幕中間顯示視圖。 – 2010-07-28 20:14:19

+0

感謝您的提示!我快到了。我修改了我的帖子以顯示我的代碼。它並不完全滑動。 – 2010-07-29 02:58:39

+0

像免費的ups應用程序一樣的視圖如何?就像通知中心視圖一樣,用戶可以拖放/滑動,甚至可以調節視圖在另一個視圖上顯示的上或下多遠。 – marciokoko 2013-01-08 17:31:17

0

看看這篇文章:http://blog.yetanotherjosh.com/post/33685102199/3-ways-to-do-a-vertical-transition-with

我正在用模態窗口的方法。

+1

請注意,[只有鏈接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info),所以答案應該是搜索解決方案的終點(而另一個引用的中途停留時間往往會隨着時間推移而過時)。請考慮在此添加獨立的摘要,並將鏈接保留爲參考。 – kleopatra 2013-10-12 09:18:56

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]; 
} 
+0

刪除動畫無法正常工作 – 2015-07-28 07:05:00

相關問題