Swift를 사용하여 iOS에서 오른쪽에서 왼쪽으로 뷰 컨트롤러를 표시하는 방법
새 화면을 표시하기 위해 presentViewController를 사용하고 있습니다.
let dashboardWorkout = DashboardWorkoutViewController()
presentViewController(dashboardWorkout, animated: true, completion: nil)
이것은 아래에서 위로 새 화면을 표시하지만을 사용하지 않고 오른쪽에서 왼쪽으로 표시하고 싶습니다 UINavigationController
.
스토리 보드 대신 Xib를 사용하고 있는데 어떻게 할 수 있습니까?
이 경우 그것은 중요하지 않습니다 xib
또는 storyboard
당신이 사용하고 있는지. 일반적으로 오른쪽에서 왼쪽으로의 전환은 뷰 컨트롤러를 발표자의 UINavigiationController
.
최신 정보
타이밍 기능 추가 kCAMediaTimingFunctionEaseInEaseOut
샘플 프로젝트 와 스위프트 4 구현은 GitHub의에 추가
Swift 3 및 4.2
let transition = CATransition()
transition.duration = 0.5
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromRight
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
present(dashboardWorkout, animated: false, completion: nil)
ObjC
CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:dashboardWorkout animated:false completion:nil];
스위프트 2.x
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
view.window!.layer.addAnimation(transition, forKey: kCATransition)
presentViewController(dashboardWorkout, animated: false, completion: nil)
이 사용자 정의 전환의 경우 메서드 의 animated
매개 변수 presentViewController
가 실제로 중요하지 않은 것처럼 보입니다 . true
또는 값 중 하나 일 수 있습니다 false
.
현재 / 해제를위한 완전한 코드, Swift 3
extension UIViewController {
func presentDetail(_ viewControllerToPresent: UIViewController) {
let transition = CATransition()
transition.duration = 0.25
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.window!.layer.add(transition, forKey: kCATransition)
present(viewControllerToPresent, animated: false)
}
func dismissDetail() {
let transition = CATransition()
transition.duration = 0.25
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
self.view.window!.layer.add(transition, forKey: kCATransition)
dismiss(animated: false)
}
}
모든 답변을 읽고 올바른 해결책을 찾을 수 없습니다. 이를 수행하는 올바른 방법은 제시된 VC 델리게이트에 대해 사용자 정의 UIViewControllerAnimatedTransitioning을 만드는 것입니다.
따라서 더 많은 단계를 수행한다고 가정하지만 결과는 더 사용자 정의가 가능하며 제시된 뷰와 함께 뷰에서 이동하는 것과 같은 부작용이 없습니다.
따라서 일부 ViewController가 있고 프레젠테이션 방법이 있다고 가정합니다.
var presentTransition: UIViewControllerAnimatedTransitioning?
var dismissTransition: UIViewControllerAnimatedTransitioning?
func showSettings(animated: Bool) {
let vc = ... create new vc to present
presentTransition = RightToLeftTransition()
dismissTransition = LeftToRightTransition()
vc.modalPresentationStyle = .custom
vc.transitioningDelegate = self
present(vc, animated: true, completion: { [weak self] in
self?.presentTransition = nil
})
}
presentTransition
그리고 dismissTransition
뷰 컨트롤러를 애니메이션에 사용됩니다. 따라서 ViewController를 UIViewControllerTransitioningDelegate
다음과 같이 채택합니다 .
extension ViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return presentTransition
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return dismissTransition
}
}
따라서 마지막 단계는 사용자 지정 전환을 만드는 것입니다.
class RightToLeftTransition: NSObject, UIViewControllerAnimatedTransitioning {
let duration: TimeInterval = 0.25
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView
let toView = transitionContext.view(forKey: .to)!
container.addSubview(toView)
toView.frame.origin = CGPoint(x: toView.frame.width, y: 0)
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
toView.frame.origin = CGPoint(x: 0, y: 0)
}, completion: { _ in
transitionContext.completeTransition(true)
})
}
}
class LeftToRightTransition: NSObject, UIViewControllerAnimatedTransitioning {
let duration: TimeInterval = 0.25
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: .from)!
container.addSubview(fromView)
fromView.frame.origin = .zero
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn, animations: {
fromView.frame.origin = CGPoint(x: fromView.frame.width, y: 0)
}, completion: { _ in
fromView.removeFromSuperview()
transitionContext.completeTransition(true)
})
}
}
해당 코드보기 컨트롤러가 현재 컨텍스트에 표시되면 해당 지점에서 사용자 지정을 수행 할 수 있습니다. 또한 사용자 정의 UIPresentationController
도 유용하다는 것을 알 수 있습니다 (사용하여 전달 UIViewControllerTransitioningDelegate
)
사용자 지정 segue를 사용할 수도 있습니다.
스위프트 3
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, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil)
}
)
}
}
이 시도,
let animation = CATransition()
animation.duration = 0.5
animation.type = kCATransitionPush
animation.subtype = kCATransitionFromRight
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
vc.view.layer.addAnimation(animation, forKey: "SwitchToView")
self.presentViewController(vc, animated: false, completion: nil)
여기에 vc
,의 ViewController입니다 dashboardWorkout
귀하의 경우.
"빠른 수정"CATransition 메서드를 사용하려는 경우 ....
class AA: UIViewController
func goToBB {
let bb = .. instantiateViewcontroller, storyboard etc .. as! AlreadyOnboardLogin
let tr = CATransition()
tr.duration = 0.25
tr.type = kCATransitionMoveIn // use "MoveIn" here
tr.subtype = kCATransitionFromRight
view.window!.layer.add(tr, forKey: kCATransition)
present(bb, animated: false)
bb.delegate, etc = set any other needed values
}
그리고 ...
func dismissingBB() {
let tr = CATransition()
tr.duration = 0.25
tr.type = kCATransitionReveal // use "Reveal" here
tr.subtype = kCATransitionFromLeft
view.window!.layer.add(tr, forKey: kCATransition)
dismiss(self) .. or dismiss(bb), or whatever
}
이 모든 것은 불행히도 실제로 정확하지 않습니다.
CATransition
이 일을하기 위해 만들어진 것이 아닙니다.
불행히도 효과를 파괴하는 성가신 크로스 페이드가 검은 색으로 변합니다 .
Many devs (like me) really don't like using NavigationController
. Often, it is more flexible to just present in an ad-hoc manner as you go, particularly for unusual and complex apps. However, it's not difficult to "add" a nav controller.
simply on storyboard, go to the entry VC and click "embed -> in nav controller". Really that's it. Or,
in
didFinishLaunchingWithOptions
it's easy to build your nav controller if you preferyou really don't even need to keep the variable anywhere, as
.navigationController
is always available as a property - easy.
Really, once you have a navigationController, it's trivial to do transitions between screens,
let nextScreen = instantiateViewController etc as! NextScreen
navigationController?
.pushViewController(nextScreen, animated: true)
and you can pop
.
That however only gives you the standard apple "dual push" effect...
(The old one slides off at a lower speed, as the new one slides on.)
Generally and surprisingly, you usually have to make the effort to do a full custom transition.
Even if you just want the simplest, most common, move-over/move-off transition, you do have to do a full custom transition.
Fortunately, to do that there's some cut and paste boilerplate code on this QA... https://stackoverflow.com/a/48081504/294884 . Happy new year 2018!
import UIKit and create one extension for UIViewController:
extension UIViewController {
func transitionVc(vc: UIViewController, duration: CFTimeInterval, type: CATransitionSubtype) {
let customVcTransition = vc
let transition = CATransition()
transition.duration = duration
transition.type = CATransitionType.push
transition.subtype = type
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
view.window!.layer.add(transition, forKey: kCATransition)
present(customVcTransition, animated: false, completion: nil)
}}
after simlpy call:
let vC = YourViewController()
transitionVc(vc: vC, duration: 0.5, type: .fromRight)
from left to right:
let vC = YourViewController()
transitionVc(vc: vC, duration: 0.5, type: .fromleft)
you can change the duration with your preferred duration...
Try this.
let transition: CATransition = CATransition()
transition.duration = 0.3
transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromLeft
self.view.window!.layer.addAnimation(transition, forKey: nil)
self.dismissViewControllerAnimated(false, completion: nil)
present view controller from right to left in iOS using Swift
func FrkTransition()
{
let transition = CATransition()
transition.duration = 2
transition.type = kCATransitionPush
transitioningLayer.add(transition,forKey: "transition")
// Transition to "blue" state
transitioningLayer.backgroundColor = UIColor.blue.cgColor
transitioningLayer.string = "Blue"
}
Refrence By : [https://developer.apple.com/documentation/quartzcore/catransition][1]
let transition = CATransition()
transition.duration = 0.25
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
tabBarController?.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.popToRootViewController(animated: true)
I have a better solution that has worked for me if you want to mantain the classic animation of Apple, without see that "black" transition that you see using CATransition(). Simply use popToViewController method.
You can look for the viewController you need in
self.navigationController.viewControllers // Array of VC that contains all VC of current stack
You can look for the viewController you need by searching for it's restorationIdentifier.
BE CAREFUL: for example, if you are navigating through 3 view controllers, and when arrive to the last you want to pop the first. You will lose, in this case, the refer to the effective SECOND view controller because the popToViewController has overwritten it. BTW, there's a solution: you can easily save before the popToViewcontroller, the VC you will need later. It worked for me very well.
This worked for me :
self.navigationController?.pushViewController(controller, animated: true)
'IT TIP' 카테고리의 다른 글
번개 모양 디자인 만들기 (예 : The Flash) (0) | 2020.10.30 |
---|---|
Android facebook applicationId는 null 일 수 없습니다. (0) | 2020.10.30 |
C #에서 SMTP를 인증하는 방법 (0) | 2020.10.30 |
Firebase 클래스에 직렬화 할 속성이 없습니다. (0) | 2020.10.30 |
속성 텍스트 중심 정렬 (0) | 2020.10.30 |