我有一个嵌入在导航控制器中的ViewController。我想要向右滑动,然后将视图控制器从VC1更改为VC2,这要归功于我设法完成stackoverflow的人员。但是,现在我想在VC2上向左滑动回到VC1时,沿传统方式向左(向右推)。
这是我在VC1中的代码(向右轻扫以转到VC2):
override func viewDidLoad() {
super.viewDidLoad()
//Swipe right gesture
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(SelectedTopicViewController.performChange))
gesture.direction = .right
self.view.addGestureRecognizer(gesture)
}
@objc func performChange() {
self.performSegue(withIdentifier: "goTo", sender: nil)
}
这是我在VC2中的代码(向左滑动即可返回VC1):
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: false)
//swipe left gesture
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(SendMessageTopicViewController.performChange))
gesture.direction = .left
self.view.addGestureRecognizer(gesture)
// Do any additional setup after loading the view.
}
@objc func performChange() {
//self.navigationController?.popViewController(animated: true)
//print("swipe detected")
self.performSegue(withIdentifier: "goBackToRight", sender: nil)
}
这也是我的自定义segue代码(用于左右滑动):
import Foundation
import UIKit
import QuartzCore
class SegueFromLeft: UIStoryboardSegue {
override func perform() {
let src: UIViewController = self.source
let dst: UIViewController = self.destination
let transition: CATransition = CATransition()
let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.duration = 0.25
transition.timingFunction = timeFunc
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
src.navigationController!.view.layer.add(transition, forKey: kCATransition)
src.navigationController!.pushViewController(dst, animated: false)
}
}
class SegueFromRight: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width*2, y: 0) //Double the X-Axis
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
}) { (finished) in
src.present(dst, animated: false, completion: nil)
}
}
}
这是我的故事板,请注意,我有一个到VC2的序列,另一个有回到VC1的序列(都是使用不同类的自定义序列)。
即使用户在VC2上滑动时,即使我有print("swiped")
,它也不会在控制台中打印出来,因此为什么我认为滑动还是有问题...
结论:在VC1上可以滑动但在VC2上选择是有效的,但是在VC2上滑动却根本不起作用...