创建自定义UIView并在Swift中显示为Pop Up [英] Creating Custom UIView and display as Pop Up in Swift

查看:131
本文介绍了创建自定义UIView并在Swift中显示为Pop Up的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的自定义 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

    })

 }

但popUp并非即将到来。我使用断点并注意到该值已分配给我的popUpView但仍然没有显示在我的主视图上。请帮助

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

请注意:我使用StoryBoard作为我的mainView和使用xib的自定义View。

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

推荐答案

在故事板中


  • 在storyboard中创建一个UIViewController

  • 将自定义类设置为anotherViewController,将Storyboard_ID设置为another_view_sid

  • 创建一个新的Cocoa Touch类作为另一个ViewController和UIVIewController的子类

在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)

在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()
        }
    })
}

这篇关于创建自定义UIView并在Swift中显示为Pop Up的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆