Swift - 翻转视图控制器禁用 IBOutlets
Swift - Flipping view controllers disables IBOutlets
我的应用启动,我在 tabBarController
的顶部 显示 VC1
tabBarController?.present(VC1, animated: false, completion: nil)
它显示第一页,我有另一页 VC2。
我通过向 tabBarController 中当前打开的 VC0 发布通知来在这两个页面之间进行翻转转换( 不是显示的那个,而是索引 0 中的那个tabBarController)。通知触发:
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
let VC2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "VC2") as! VC2
UIView.transition(from: topController.view, to: VC2.view, duration: 0.85, options: [.transitionFlipFromLeft])
}
这实现了翻转,但看起来 IBOutlets 未连接且未检测到点击。
如果是 VC2,您还没有保留任何参考资料。所以 IBOutlet 的动作不执行。你可以试试这个例子。可能对你有帮助。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var allViewInstantController: [UIViewController]=[UIViewController]()
public static let NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME:Int = 5
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
static func transitionViewFrom(from:UIViewController, to:UIViewController, subtype:CATransitionSubtype? = nil, options: UIView.AnimationOptions? = nil) -> Void {
let timeDelay:Double=0.5
GlobalVariable.checkPreviousView(_view:to)
GlobalVariable.splashViewFrom(from: from, to:to, isAdd:true, subtype:subtype, options: options, timeDelay: timeDelay)
}
static func checkPreviousView(_view:UIViewController) -> Void {
GlobalVariable.shared().allViewInstantController.append(_view)
if(GlobalVariable.shared().allViewInstantController.count>GlobalVariable.NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME){
let myViewController = GlobalVariable.shared().allViewInstantController.first
myViewController?.view.removeFromSuperview()
GlobalVariable.shared().allViewInstantController.removeFirst()
}
}
static func splashViewFrom(from:UIViewController, to:UIViewController, isAdd:Bool, subtype:CATransitionSubtype? = nil, options: UIView.AnimationOptions? = nil, timeDelay:Double) -> Void {
do {
if let subtype = subtype {
let transition = CATransition()
transition.duration = 0.25
transition.isRemovedOnCompletion = true;
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = subtype
from.view.layer.add(transition, forKey: kCATransition)
}else if let options = options {
UIView.transition(from: from.view, to: to.view, duration: timeDelay, options: options)
}
if(!isAdd){
from.view.removeFromSuperview()
if(GlobalVariable.shared().allViewInstantController.count>0){
GlobalVariable.shared().allViewInstantController.removeLast()
}
}else{
if subtype != nil {
from.view.addSubview(to.view)
}
}
}
}
static func dismissViewFrom(viewController:UIViewController, subtype:CATransitionSubtype) -> Void {
let timeDelay=0.5
GlobalVariable.splashViewFrom(from: viewController, to: UIViewController(), isAdd:false, subtype:subtype, timeDelay: timeDelay)
}
}
第二ViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
翻转只需更改类型。并相应地更改后页交易。
transition.type = CATransitionType(rawValue: "flip")
另一种解决方案,你可以试试这个。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var rootViewController: UIViewController?
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
}
第二ViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}
我的应用启动,我在 tabBarController
的顶部 显示 VC1tabBarController?.present(VC1, animated: false, completion: nil)
它显示第一页,我有另一页 VC2。 我通过向 tabBarController 中当前打开的 VC0 发布通知来在这两个页面之间进行翻转转换( 不是显示的那个,而是索引 0 中的那个tabBarController)。通知触发:
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
let VC2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "VC2") as! VC2
UIView.transition(from: topController.view, to: VC2.view, duration: 0.85, options: [.transitionFlipFromLeft])
}
这实现了翻转,但看起来 IBOutlets 未连接且未检测到点击。
如果是 VC2,您还没有保留任何参考资料。所以 IBOutlet 的动作不执行。你可以试试这个例子。可能对你有帮助。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var allViewInstantController: [UIViewController]=[UIViewController]()
public static let NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME:Int = 5
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
static func transitionViewFrom(from:UIViewController, to:UIViewController, subtype:CATransitionSubtype? = nil, options: UIView.AnimationOptions? = nil) -> Void {
let timeDelay:Double=0.5
GlobalVariable.checkPreviousView(_view:to)
GlobalVariable.splashViewFrom(from: from, to:to, isAdd:true, subtype:subtype, options: options, timeDelay: timeDelay)
}
static func checkPreviousView(_view:UIViewController) -> Void {
GlobalVariable.shared().allViewInstantController.append(_view)
if(GlobalVariable.shared().allViewInstantController.count>GlobalVariable.NUMBER_OF_VIEWCONTROLLER_OPEN_AT_A_TIME){
let myViewController = GlobalVariable.shared().allViewInstantController.first
myViewController?.view.removeFromSuperview()
GlobalVariable.shared().allViewInstantController.removeFirst()
}
}
static func splashViewFrom(from:UIViewController, to:UIViewController, isAdd:Bool, subtype:CATransitionSubtype? = nil, options: UIView.AnimationOptions? = nil, timeDelay:Double) -> Void {
do {
if let subtype = subtype {
let transition = CATransition()
transition.duration = 0.25
transition.isRemovedOnCompletion = true;
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = subtype
from.view.layer.add(transition, forKey: kCATransition)
}else if let options = options {
UIView.transition(from: from.view, to: to.view, duration: timeDelay, options: options)
}
if(!isAdd){
from.view.removeFromSuperview()
if(GlobalVariable.shared().allViewInstantController.count>0){
GlobalVariable.shared().allViewInstantController.removeLast()
}
}else{
if subtype != nil {
from.view.addSubview(to.view)
}
}
}
}
static func dismissViewFrom(viewController:UIViewController, subtype:CATransitionSubtype) -> Void {
let timeDelay=0.5
GlobalVariable.splashViewFrom(from: viewController, to: UIViewController(), isAdd:false, subtype:subtype, timeDelay: timeDelay)
}
}
第二ViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
GlobalVariable.transitionViewFrom(from: self, to: vc, subtype: nil, options: [.transitionFlipFromLeft])
}
}
翻转只需更改类型。并相应地更改后页交易。
transition.type = CATransitionType(rawValue: "flip")
另一种解决方案,你可以试试这个。
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func gotoNextPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}
class GlobalVariable: NSObject {
private static var sharedManager: GlobalVariable!
public var rootViewController: UIViewController?
static func shared() -> GlobalVariable {
if(sharedManager==nil){
sharedManager=GlobalVariable()
}
return sharedManager
}
override private init() {
}
}
第二ViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GlobalVariable.shared().rootViewController=self
}
@IBAction func previousPage(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
UIView.transition(from: self.view, to: vc.view, duration: 0.5, options: [.transitionFlipFromLeft])
}
}