2010-07-28 35 views
9

Tôi muốn một UIView trượt lên từ dưới cùng của màn hình (và ở giữa màn hình) như một UIActionSheet. Làm thế nào tôi có thể thực hiện điều này?Làm thế nào tôi có thể trình bày một UIView từ dưới cùng của màn hình như một UIActionSheet?

UPDATE: Tôi đang sử dụng đoạn mã sau:

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

Quan điểm dường như được sinh động từ góc và xuất hiện trong góc. Làm thế nào tôi có thể làm cho nó trượt lên từ phía dưới? Đến gần!

+0

cập nhật bài với một số mã mà nên giúp – iwasrobbed

+0

một ví dụ tốt: http://github.com/homeyer/CustomUIActionSheet –

Trả lời

4

Làm những gì Matt đã làm ở đây, nhưng chỉ thay đổi giá trị và hướng. Tôi có mã ở nhà để làm điều này từ phía dưới nếu bạn cần nó sau này (tôi sẽ cập nhật bài đăng này).

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

Ngoài ra, đừng quên đưa ra các bit mã mà chuyển giao diện chính xuống (nên thay vì các UIView chỉ bật trên đầu như một ActionSheet)

cập nhật với mã:

Đây là những gì tôi sử dụng trong một trong những ứng dụng của tôi để hiển thị/ẩn một chút "tùy chọn" xem:

- (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

Tôi thích giải pháp của bạn. Có cách nào bạn ngăn người dùng tương tác với chế độ xem đằng sau cửa sổ bật lên này không? –

+0

Một cách đơn giản là chỉ cần vô hiệu hóa tương tác của người dùng (nghĩa là '[someView setUserInteractionEnabled: NO];') của chế độ xem đó. Một cách tốt hơn là hiển thị lần đầu tiên chế độ xem khác với một số loại gradient bán trong suốt với tương tác người dùng bị vô hiệu hóa và * sau đó * tạo hoạt ảnh cho chế độ xem mới của bạn. Bằng cách này, người dùng có ý nghĩa rằng vì chế độ xem khác bị mờ đi một chút, nên họ không thể tương tác với nó nữa. Nếu không họ có thể nghĩ rằng một cái gì đó bị hỏng. Chỉ cần nghĩ về cách UIAlertView thực hiện điều này. Khi bạn đã hoàn tất, chỉ cần xóa cả hai chế độ xem – iwasrobbed

1

Bạn sẽ phải tự di chuyển chế độ xem, bằng cách đặt center hoặc frame. Tôi sẽ cho bạn biết những gì để thiết lập chúng. Nhưng đối với các hình ảnh động:

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

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

Thanks for the tip! Tôi gần tới rồi. Tôi đã sửa đổi bài đăng của mình để hiển thị mã của tôi. Nó không phải là chính xác trượt lên. –

4

Một cách sẽ được sử dụng bộ điều khiển xem phương thức có mặt trên bộ điều khiển xem:

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

Để biết thêm thông hãy nhìn vào các UIViewController documentation.

EDIT: Nếu bạn muốn xem ở giữa màn hình, bạn sẽ cần tạo hiệu ứng động vào vị trí như @jtbandes đã chỉ ra. Tôi cũng khuyên bạn nên thêm một số khối kẹo vào khối hoạt ảnh UIView:

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

myView.center = CGPointMake(x,y); 

[UIView commitAnimations]; 

Sau đó, bạn có thể di chuyển lại nếu bạn cần toàn màn hình hoặc loại bỏ nó.

+0

Tôi cũng muốn nó hoạt động giống như một UIActionSheet, bằng cách hiển thị khung nhìn giữa màn hình. –

+0

Cảm ơn mẹo! Tôi gần tới rồi. Tôi đã sửa đổi bài đăng của mình để hiển thị mã của tôi. Nó không phải là chính xác trượt lên. –

+0

Làm thế nào về một chế độ xem như ứng dụng up miễn phí?Nó giống như chế độ xem trung tâm thông báo mà người dùng có thể kéo/trượt và thậm chí có thể điều chỉnh mức độ hiển thị lên hoặc xuống trên chế độ xem khác. – marciokoko

0

Khám phá bài đăng này: http://blog.yetanotherjosh.com/post/33685102199/3-ways-to-do-a-vertical-transition-with

Tôi sẽ sử dụng phương pháp tiếp cận cửa sổ phương thức.

+1

Lưu ý rằng [câu trả lời chỉ có liên kết được khuyến khích] (http://meta.stackoverflow.com/tags/link-only-answers/info), các câu trả lời SO phải là điểm cuối của một tìm kiếm cho một giải pháp (so với một điểm dừng khác của tài liệu tham khảo, mà có xu hướng để có được cũ theo thời gian). Vui lòng xem xét thêm bản tóm tắt độc lập tại đây, giữ liên kết dưới dạng tham chiếu. – kleopatra

0

Hãy thử giải pháp này .... nó hoạt động

#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

Xóa hoạt ảnh không hoạt động bình thường –

Các vấn đề liên quan