我一直试图在过去的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 :(得分:1)
首先,创建灰色空视图
self.tableView.contentOffset = CGPointMake(0, self.tableView.contentOffset.y + 44);
其次,将刚刚创建的视图设置为叠加层的背景
func makeGrayView() -> UIView {
var view = UIView(frame:UIScreen.mainScreen().applicationFrame)
self.view.backgroundColor = UIColor.greyColor()
return view
}
答案 1 :(得分:0)
您必须使用Cusotm UIViewControllerAnimation
类来实现此目的。使用上面的代码。创建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/
答案 2 :(得分:0)
您可以使用故事板来完成此操作。首先将一个按钮拖到第一个UIViewController上。
然后将按钮连接到这样的动作
@IBAction func presentForm() {
self.view.backgroundColor = UIColor.lightGrayColor()
}
Ok,其次,将另一个UIViewController拖到故事板上,然后按住Ctrl键拖动到该控制器以创建一个segue。使用我在下面的相同设置。提出模态和全屏。
确定接下来是在第二个ViewController上创建表单。我在下面添加了一个简单的。下一步是在第二个视图控制器上选择视图。这是将背景视图颜色更改为“清除”颜色。图像显示层次结构中的视图和选择为“清除”的背景颜色。
然后在表格元素所在的位置拖动另一个视图。就像我的标签和文本字段一样。在构建和运行项目时,您将拥有以下屏幕。
请注意,您必须添加自己的自动布局限制,但它们很简单。你也可以玩代码。就像更改按钮上的第一个Viewcontroller的不透明值一样。这应该足以让你入门。