创建自定义UIView并在Swift中弹出显示
我正在尝试创建一个自定义UIView
并将其显示为使用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)
}
}
我在我的主要观点中称它为:
@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
})
}
但弹出不来。 我使用断点,并注意到值被分配给我的popUpView,但仍然不会显示在我的主视图。 请帮忙
请注意:我使用StoryBoard作为我使用xib制作的mainView和自定义View。
将视图呈现为模态轮廓
看看这个教程:
http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/
您可以使用系统提供的segue转换或自定义的segue转换来获得真正的花式效果。
以下是制作自定义过渡动画制作者的一种方法:
//
// 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)
}
}
在故事板中
在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)
在另一个ViewController中
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/25231.html