草庐IT

ios - 交互式关闭 ViewController

coder 2023-09-17 原文

我有一个这样的 Storyboard:

文章 View 由转场和动画呈现:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if segue.identifier == "showArticleFromArticles" {
        let ViewToShow = segue.destinationViewController as! ArticleView
        ViewToShow.articleToShow = ArticleToShow2
        ViewToShow.transitioningDelegate = self
    }

}

我的动画:

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerInteractiveTransitioning, UIViewControllerTransitioningDelegate, UIViewControllerContextTransitioning {

weak var transitionContext: UIViewControllerContextTransitioning?

var sourceViewController: UIViewController! {
    didSet {
        print("set")
        print(sourceViewController)
        enterPanGesture = UIScreenEdgePanGestureRecognizer()
        enterPanGesture.addTarget(self, action:"panned:")
        enterPanGesture.edges = UIRectEdge.Left
        let newSource = sourceViewController as! ArticleView
        newSource.WebView.addGestureRecognizer(enterPanGesture)
    }
}

let duration    = 1.0
var presenting  = true
var originFrame = CGRectNull

private var didStartedTransition = false
private var animated = false
private var interactive = false
private var AnimationStyle = UIModalPresentationStyle(rawValue: 1)
private var didFinishedTransition = false
private var percentTransition: CGFloat = 0.0
private var enterPanGesture: UIScreenEdgePanGestureRecognizer!

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    // set up from 2D transforms that we'll use in the animation
    let offScreenRight = CGAffineTransformMakeTranslation(container!.frame.width, 0)
    let offScreenLeft = CGAffineTransformMakeTranslation(container!.frame.width, 0)

    // start the toView to the right of the screen
    toView.transform = offScreenRight

    // add the both views to our view controller
    container!.addSubview(toView)
    container!.addSubview(fromView)

    // get the duration of the animation
    // DON'T just type '0.5s' -- the reason why won't make sense until the next post
    // but for now it's important to just follow this approach
    let duration = self.transitionDuration(transitionContext)

    // perform the animation!
    // for this example, just slid both fromView and toView to the left at the same time
    // meaning fromView is pushed off the screen and toView slides into view
    // we also use the block animation usingSpringWithDamping for a little bounce
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {

        fromView.transform = offScreenLeft
        toView.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return duration
}

func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning) {
    interactive = true


    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    // set up from 2D transforms that we'll use in the animation
    let offScreenRight = CGAffineTransformMakeTranslation(container!.frame.width, 0)
    let offScreenLeft = CGAffineTransformMakeTranslation(container!.frame.width, 0)

    // start the toView to the right of the screen
    toView.transform = offScreenRight

    // add the both views to our view controller
    container!.addSubview(toView)
    container!.addSubview(fromView)

    // get the duration of the animation
    // DON'T just type '0.5s' -- the reason why won't make sense until the next post
    // but for now it's important to just follow this approach
    let duration = self.transitionDuration(transitionContext)

    // perform the animation!
    // for this example, just slid both fromView and toView to the left at the same time
    // meaning fromView is pushed off the screen and toView slides into view
    // we also use the block animation usingSpringWithDamping for a little bounce
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: {

        fromView.transform = offScreenLeft
        toView.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })


}

func containerView() -> UIView? {
    return sourceViewController?.view
}

func viewControllerForKey(key: String) -> UIViewController? {
    return sourceViewController?.storyboard!.instantiateViewControllerWithIdentifier(key)
}

func viewForKey(key: String) -> UIView? {
    return sourceViewController?.storyboard!.instantiateViewControllerWithIdentifier(key).view
}

func initialFrameForViewController(vc: UIViewController) -> CGRect {
    return vc.view.frame
}

func finalFrameForViewController(vc: UIViewController) -> CGRect {
    return vc.view.frame
}

func isAnimated() -> Bool {
    return animated
}

func isInteractive() -> Bool {
    return interactive
}

func presentationStyle() -> UIModalPresentationStyle {
    return AnimationStyle!
}

func completeTransition(didComplete: Bool) {
    didFinishedTransition = didComplete
}

func updateInteractiveTransition(percentComplete: CGFloat) {
    percentTransition = percentComplete
}

func finishInteractiveTransition() {
    completeTransition(true)
}

func cancelInteractiveTransition() {
    completeTransition(true)
}

func transitionWasCancelled() -> Bool {
    return didFinishedTransition
}

func targetTransform() -> CGAffineTransform {
    return CGAffineTransform()
}

func panned(pan: UIPanGestureRecognizer) {
    //print(pan.translationInView(sourceViewController!.view))
    switch pan.state {
    case .Began:
        animated = true
        didStartedTransition = true
        didFinishedTransition = false
        sourceViewController?.dismissViewControllerAnimated(true, completion: nil)
        if transitionContext != nil {
            startInteractiveTransition(transitionContext!)
        }
        break
    case .Changed:
        percentTransition = CGFloat(pan.translationInView(sourceViewController!.view).x / sourceViewController!.view.frame.width)
        print(percentTransition)
        updateInteractiveTransition(percentTransition)
        break
    case .Ended, .Failed, .Cancelled:
        animated = false
        didStartedTransition = false
        didFinishedTransition = true
        finishInteractiveTransition()
        break
    case .Possible:
        break
    }
}

在文章 View 中,我这样调用关闭 View :

@IBAction func Quit(sender: UIBarButtonItem) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

和:

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.presenting = false
    return transition
}

然后我像那样添加 PanGesture :

let transition = TransitionManager()
self.transition.sourceViewController = self

但 Pan Gesture 只是关闭 View ,并且 Interactive 不可用 因为我打电话:

self.dismissViewControllerAnimated(true, completion: nil)

在 UIPanGestureRecognizer.began 期间

我该怎么做?

我正在使用 Xcode 7、Swift 2、iOS 9

谢谢!

最佳答案

我找到了解决方案:

我应该只用

startInteractiveTransition

实例化一些东西

并使用:

func updateInteractiveTransition(percentComplete: CGFloat) {
    if self.reverse {
        print(percentComplete)
        self.tovc.view.frame.origin.x = (self.fromvc.view.frame.maxX * (percentComplete)) - self.fromvc.view.frame.maxX
    }
}

自定义我的过渡。

关于ios - 交互式关闭 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295865/

有关ios - 交互式关闭 ViewController的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. ruby - 如何关闭 ruby​​ gem "Spreadsheet?"中的文件 - 2

    下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby-on-rails - Ruby 的 'open_uri' 是否在读取或失败后可靠地关闭套接字? - 2

    一段时间以来,我一直在使用open_uri下拉ftp路径作为数据源,但突然发现我几乎连续不断地收到“530抱歉,允许的最大客户端数(95)已经连接。”我不确定我的代码是否有问题,或者是否是其他人在访问服务器,不幸的是,我无法真正确定谁有问题。本质上,我正在读取FTPURI:defself.read_uri(uri)beginuri=open(uri).readuri=="Error"?nil:urirescueOpenURI::HTTPErrornilendend我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施来关闭所有连接,这样我的连接就不是问题所在,但是我

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  9. ruby - 如何在 watir 测试套件结束时关闭浏览器? - 2

    使用ruby​​的watir测试网络应用程序时,浏览器最后会保持打开状态。网上的一些建议是,要进行真正的单元测试,您应该在每次测试时(在拆卸调用中)打开和关闭浏览器,但这很慢而且毫无意义。或者他们做这样的事情:defself.suites=superdefs.afterClass#Closebrowserenddefs.run(*args)superafterClassendsend但这会导致摘要输出不再显示(诸如“100次测试、100次断言、0次失败、0次错误”之类的内容仍应显示)。我怎样才能让ruby​​或watir在我的测试结束时关闭浏览器? 最佳答案

  10. ruby-on-rails - 如何在一段时间后关闭 Rails 闪现消息? - 2

    我想设置秒数aflash在自动关闭之前向用户显示通知。 最佳答案 您可以在页面上使用一些简单的JavaScript(在此示例中使用jQuery):$('document').ready(function(){setTimeout(function(){$('#flash').slideUp();},3000);});假设保存您的flash消息的HTML元素的id是#flash,这将向上滑动并在3000毫秒(3秒)后将其隐藏。 关于ruby-on-rails-如何在一段时间后关闭Rails

随机推荐