Creating Custom UIView and display as Pop Up in Swift

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

My Custom UIView code is

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

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


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/

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

尝试修改你的视图到这样的事情:

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

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
  • 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()
            }
        })
    }
    
    链接地址: http://www.djcxy.com/p/25232.html

    上一篇: 在javascript客户端和java服务器之间共享Google Analytics客户端ID

    下一篇: 创建自定义UIView并在Swift中弹出显示