我正在尝试创建一个自定义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
})
}
但popUp并非即将到来。我使用断点并注意到该值已分配给我的popUpView但仍然没有显示在我的主视图上。请帮忙
请注意:我使用StoryBoard作为我的mainView和我使用xib进行的自定义View。
答案 0 :(得分:2)
在故事板中
在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()
}
})
}
答案 1 :(得分:1)
将视图显示为模态segue
查看本教程:
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())
})
}
}
}
答案 2 :(得分:0)
在DatePopUpView中,您似乎正在加载nib文件,但实际上并未将其作为子视图添加到视图中。
答案 3 :(得分:0)
迅捷的Mili Shah答案5版本
let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController
self.addChild(popvc)
popvc.view.frame = self.view.frame
self.view.addSubview(popvc.view)
popvc.didMove(toParent: self)
@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(toParent: nil)
self.view.removeFromSuperview()
self.removeFromParent()
}
})
}