2015-09-07 69 views
0

我一直試圖在最後3個小時內完成此任務,但我無法弄清楚如何執行此操作。任何人都可以幫忙嗎?將模態視圖放在視圖上並將背景變爲灰色

所以這就是我想要做的。當我按下一個按鈕時,比如說一個Sign In按鈕,我想要一個模式視圖彈出來,使它背後的視圖變成灰色且無法使用。在這個模式視圖中,我需要幾個按鈕和靜態標籤。

我已閱讀並嘗試瞭解已有的幾種資源,例如:Present modal view controller in half size parent controller,http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/,How to use modal views in swift?和其他幾個。但是,我很難理解代碼。

到目前爲止,我有這個代碼是應該做的模態視圖是在它後面的視圖的頂部:

@IBAction func signIn(sender: AnyObject) { 
    self.modalTransitionStyle = UIModalTransitionStyle.CoverVertical 
    // Cover Vertical is necessary for CurrentContext 
    self.modalPresentationStyle = .CurrentContext 
    // Display on top of current UIView 
    self.presentViewController(SignInViewController(), animated: true, completion: nil) 
} 

但這不生產,我想要的效果。有人請幫忙嗎?

回答

0

首先,創建你的灰色空視圖

func makeGrayView() -> UIView { 
    var view = UIView(frame:UIScreen.mainScreen().applicationFrame) 
    self.view.backgroundColor = UIColor.greyColor() 
    return view 
} 

二,設置您剛纔創建爲您的覆蓋

var backView = self.makeGrayView() 
self.view.addSubview(backView) 
+0

您能否在Swift中提供代碼?我是一個超級noob ... – Jae

+0

@Jae我編輯了答案。希望這可以幫助。 – Dree

0

你必須使用Cusotm UIViewControllerAnimation類來實現這一目標的背景圖。使用上面的代碼.Create TransparentTransition.h and .m File

#import <Foundation/Foundation.h> 

@interface TransparentTransition :NSObject<UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate>{ 
BOOL isPresenting; 
} 
@end 

//TransparentTransition.m

#import "TransparentTransition.h" 

@implementation TransparentTransition 
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{ 
return 0.8f; 
} 

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{ 

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
UIView *toView = toVC.view; 
UIView *fromView = fromVC.view; 

UIView* containerView = [transitionContext containerView]; 


// Position the presented view off the top of the container view 

if(isPresenting){ 
    [containerView addSubview:toView]; 
    toView.frame = CGRectMake(0, -toView.frame.size.height, toView.frame.size.width, toView.frame.size.height); 
    [UIView animateWithDuration:[self transitionDuration:transitionContext] 
          delay:0 
     usingSpringWithDamping:.8 
      initialSpringVelocity:6.0 
         options:0 
        animations:^{ 
          toView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"black_patch.png"]]; 
         CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); 
         CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(0.f,+toView.frame.size.height); 
         toView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); 


        } 
        completion:^(BOOL finished){ 
         [transitionContext completeTransition:YES]; 

        }]; 

} 
else{ 

    [UIView animateWithDuration:[self transitionDuration:transitionContext] 
          delay:0 
         options:0 
        animations:^{ 
         //        toView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"black_patch.png"]]; 
         CGAffineTransform scaleTrans = CGAffineTransformMakeScale(1.0f, 1.0f); 
         CGAffineTransform lefttorightTrans = CGAffineTransformMakeTranslation(0,-fromView.frame.size.height); 
         fromView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans); 


        } 
        completion:^(BOOL finished){ 
         [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
        }]; 


    } 

    } 



- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{ 

isPresenting=YES; 
return self; 
} 

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{ 
isPresenting=NO; 
return self; 
} 

- (UIDynamicAnimator*)animateForTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext { 
// Has to be implemented by subclasses 
return nil; 
} 

以上動畫

var transitionManager:TransparentTransition 
@IBAction func signIn(sender: AnyObject) { 

let detail = self.storyboard?.instantiateViewControllerWithIdentifier("detail") as! DetailViewController 
    detail.modalPresentationStyle = UIModalPresentationStyle.Custom; 
    detail.transitioningDelegate = transitionManager; 
self.presentViewControllerdetail, animated: true, completion: nil) 
} 

而且,這裏的//用法是很好的教程,介紹如何進行這樣的酷動畫 http://www.appcoda.com/custom-view-controller-transitions-tutorial/

+0

您能否在Swift中提供代碼? – Jae

+0

你可以創建一個獨立的動畫文件,我提供的代碼和導入該文件在Objective-C橋頭和使用。我已經編輯了有關在swift中使用的文章。它是一個很大的文件,我不能在迅速翻譯它現在。 – Mukesh

0

你可以用做故事板。首先將一個按鈕拖到第一個UIViewController上。 enter image description here

然後將按鈕連接到一個動作,像這樣

@IBAction func presentForm() { 
    self.view.backgroundColor = UIColor.lightGrayColor() 
} 

好的其次,將另一個的UIViewController到故事板和Ctrl按鈕拖動到該控制器以創建SEGUE。使用下面的相同設置。全屏顯示模式爲&。 enter image description here

好下一步是在第二個ViewController上創建你的表單。我在下面添加了一個簡單的。下一步是在第二個視圖控制器上選擇視圖。這是將背景視圖顏色更改爲清除顏色。該圖像將顯示層次結構中的視圖以及選擇爲「清除」的背景顏色。 enter image description here

然後在窗體元素所在的位置拖動另一個視圖。就像我擁有的​​標籤和文本框一樣。當您構建並運行您的項目時,您將擁有以下屏幕。enter image description hereenter image description here

請注意,您必須添加自己的自動佈局約束,但它們很簡單。你也可以玩代碼。就像改變按鈕上的第一個Viewcontroller的不透明值等。這應該足以讓你開始。

相關問題