ios 在 Swift 中创建自定义 UIView 并显示为 Pop Up

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29204112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:18:25  来源:igfitidea点击:

Creating Custom UIView and display as Pop Up in Swift

iosiphoneswiftuiview

提问by Shruti

I am trying to create a custom UIViewand display it as a pop up in my main View using Swift.

我正在尝试创建一个自定义UIView并将其显示为使用 Swift 的主视图中的弹出窗口。

My Custom UIViewcode is

我的自定义UIView代码是

class DatePopUpView: UIView {
var uiView:UIView?

override init()  {
    super.init()
    self.uiView = NSBundle.mainBundle().loadNibNamed("DatePopUpView", owner: self, options: nil)[0] as? UIView

}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
      }

required override init(frame: CGRect) {
           super.init(frame: frame)

}

}

And I am Calling it in my main view as:

我在我的主要观点中称它为:

 @IBAction func date_button_pressed (sender : AnyObject?) {
 var popUpView = DatePopUpView()
 var centre : CGPoint = CGPoint(x: self.view.center.x, y: self.view.center.y)

    popUpView.center = centre
    popUpView.layer.cornerRadius = 10.0
  let trans = CGAffineTransformScale(popUpView.transform, 0.01, 0.01)
    popUpView.transform = trans
    self.view .addSubview(popUpView)
    UIView .animateWithDuration(0.5, delay: 0.0, options:     UIViewAnimationOptions.CurveEaseInOut, animations: {

        popUpView.transform = CGAffineTransformScale(popUpView.transform, 100.0, 100.0)

        }, completion: {
            (value: Bool) in

    })

 }

But popUp is not Coming. I used breakpoint and noticed that value is getting assigned to my popUpView but still it is not displayed on my main View. Please Help

但是弹出没有来。我使用了断点并注意到值已分配给我的 popUpView,但它仍然没有显示在我的主视图上。请帮忙

Please Note: I am using StoryBoard for my mainView and custom View i have made using xib.

请注意:我将 StoryBoard 用于我的 mainView 和我使用 xib 制作的自定义视图。

回答by dragonball

try to modify your View to something like this:

尝试将您的视图修改为如下所示:

class DatePopUpView: UIView {
     var uiView:UIView?
     override init()  {
         super.init()
         self.setup()
     }
     required init(coder aDecoder: NSCoder)  {
         super.init(coder: aDecoder)
         self.setup()
     }
     override init(frame: CGRect)   {
         super.init(frame: frame)
         self.setup()
     }
     setup() {
         self.uiView = NSBundle.mainBundle().loadNibNamed("DatePopUpView", owner: self, options: nil)[0] as? UIView
         self.uiView.frame = self.bounds
         self.uiView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
         self.addSubview(self.uiView)
     }
}

回答by Mili Shah

In storyboard

在故事板中

  • In storyboard create one UIViewController
  • Set custom class as anotherViewController and Storyboard_ID as another_view_sid
  • Create one new Cocoa Touch Class as anotherViewController and subclass of UIVIewController
  • 在故事板中创建一个 UIViewController
  • 将自定义类设置为 anotherViewController 并将 Storyboard_ID 设置为 another_view_sid
  • 创建一个新的 Cocoa Touch 类作为 anotherViewController 和 UIVIewController 的子类

In viewController

在视图控制器中

    let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController

    self.addChildViewController(popvc)

    popvc.view.frame = self.view.frame

    self.view.addSubview(popvc.view)

    popvc.didMove(toParentViewController: self)

In anotherViewController

在另一个视图控制器中

override func viewDidLoad() {
    super.viewDidLoad()
        showAnimate()
    }
}

@IBAction func Close_popupView(_ sender: Any) {
    removeAnimate()
}

func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0
    }, completion: {(finished : Bool) in
        if(finished)
        {
            self.willMove(toParentViewController: nil)
            self.view.removeFromSuperview()
            self.removeFromParentViewController()
        }
    })
}

回答by clearlight

Present the view as a modal segue

将视图呈现为模态转场

Check out this tutorial:

看看这个教程:

http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/

http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/

You can use system provided segue transitions or make custom segue transitions to get really fancy effects.

您可以使用系统提供的转场转场或自定义转场转场以获得真正奇特的效果。

Here's one approach to making custom transition animators:

这是制作自定义过渡动画的一种方法:

//
//  BaseTransitionAnimator.swift
//

import UIKit

class BaseTransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning {

    enum PresentationMode {
        case Presenting, Dismissing
    }
    var duration     : NSTimeInterval = 1.0
    var mode         : PresentationMode = .Presenting

    init(duration: NSTimeInterval, mode: PresentationMode) {
        self.duration = duration
        self.mode = mode
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return duration
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // stub - must be overriden by inheritor
    }


//
//  ExpandingTransitionAnimator.swift
//

import UIKit

class ExpandingTransitionAnimator : BaseTransitionAnimator {

    enum ExpansionMode {
        case Basic, WithFadingImage
    }

    var image : UIImage? = nil

    override init(duration: NSTimeInterval, mode: PresentationMode) {
        super.init(duration: duration, mode: mode)
    }

    override func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        var fromVC             = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        var toVC               = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        var fromView           = fromVC.view
        var toView             = toVC.view
        var containerView      = transitionContext.containerView()
        var duration           = transitionDuration(transitionContext)
        var initialFrame       = transitionContext.initialFrameForViewController(fromVC)

        var imageView : UIImageView?
        if image != nil {
            imageView = UIImageView(image: image!.crop(CGPointMake(0, 0),  size: toView.frame.size))

        }
        if (mode == .Presenting) {  
                toView.transform = CGAffineTransformMakeScale(0.05, 0.05)
               var originalCenter = toView.center
                containerView.addSubview(toView)
                if imageView != nil {
                    imageView!.transform = CGAffineTransformMakeScale(0.05, 0.05);
                    containerView.addSubview(imageView!)
                }

                UIView.animateWithDuration(duration,
                    delay:0.0,
                    options:.CurveEaseInOut,
                    animations: {
                        if imageView != nil {
                            imageView!.alpha = 0.0
                            imageView!.transform =  CGAffineTransformMakeScale(1.0, 1.0)
                        }
                        toView.transform =  CGAffineTransformMakeScale(1.0, 1.0)
                        toView.center = originalCenter
                    },
                    completion: { _ in
                        transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
                })
        } else { // dismissing
            UIView.animateWithDuration(duration,
                animations: {
                    fromView.transform = CGAffineTransformMakeScale(0.05, 0.05)
                    fromView.center = toView.center
                },
                completion: { _ in
                    fromView.removeFromSuperview()
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
            })
        }
    }
}

回答by Deepthi Chand

In the DatePopUpView looks like you are loading the nib file but not actually adding as subview to the view.

在 DatePopUpView 中,您似乎正在加载 nib 文件,但实际上并未将其作为子视图添加到视图中。