jjzjj

ios - 快速向 presentViewController 添加自定义转换

coder 2023-09-14 原文

我有一个 viewController,它有一个 TableView 和一个 accessoryTypedetailbutton 的单元格。 当按下 accessoryType 时,我会呈现一个带有文本的半透明 View 。

我希望此 View 以特定方式显示,因此我创建了一个自定义 View Controller 。

但是,尝试在 detailbutton 中运行该 viewController 是行不通的。

我的部分代码:

//Defined in the class
let customTransitionManager = TransitionManager()

//in tableView cellForRowAtIndexPath
if (tableView == continent){
        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: "continentSelector")
        cell.textLabel?.text = "Continent"
        cell.accessoryType = UITableViewCellAccessoryType.DetailButton;
        return cell


func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
    println(indexPath)
    let detailController = storyboard?.instantiateViewControllerWithIdentifier("detailButtonStoryBoard") as? detailedView
    detailController?.modalPresentationStyle = .Custom
    detailController?.transitioningDelegate = customTransitionManager;
    presentViewController(detailController!, animated: true, completion: nil)
}

运行此代码会出现以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

以下突出显示:

-> 0x100209cf0 <function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()+44>: brk    #0x1

最佳答案

我看到有两个地方可以使用 nil 值。第一个是在索引路径处的行的单元格中,但这不太可能。第二个也是更有可能的一个是您在呈现 detailContoller 时尝试解包的地方。您是否确认您正在从 Storyboard 中取回 Controller ?

更新:我在评论中提出的解决方案格式很糟糕,这里有一个更好的格式

找到解决方案使用 segue 而不是手动呈现 Controller 。

  1. 从 didSelectRowAtIndexPath 调用 performSegue
  2. 在 prepareForSegue 方法中放入:

    let toViewController = segue.destinationViewController as UIViewController
    toViewController.transitioningDelegate = self.customTransitionManager`
    

本教程的全部功劳:http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations

更新 2:下载您的项目并使其运行。以下是我所做的更改:

  1. 我在牛仔裤 Controller 和细节 Controller 之间添加了一个模态转场。我称之为“DetailSegue”
  2. 我更新了 jeans.swift 文件的 accessoryButtonTappedForRowWithIndexPath 方法如下:

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
        println(indexPath)
        self.performSegueWithIdentifier("DetailSegue", sender: self)
    }
    
  3. 然后我按如下方式更新了 TransitionManger animateTransition 函数,基本上使往返 View 成为可选的并适本地处理 nil

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //get reference to our FROM View, To View, and container view that we should perform the transition in
        let container = transitionContext.containerView()
    
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
    
        //setup from 2D transform that we'll use in the animation
        let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
        let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)
    
        //start the view to the right of the screen 
        toView?.transform = offScreenRight
    
        //add both the views to our view controller
        if toView != nil {
            container.addSubview(toView!)
        }
    
        if fromView != nil {
            container.addSubview(fromView!)
        }
    
        // get the duration of the animation
        let duration = self.transitionDuration(transitionContext)
    
        //perform the animation
    
        UIView.animateWithDuration(duration, delay: 0.0, options: nil, animations: {
            fromView?.transform = offScreenLeft
            toView?.transform = CGAffineTransformIdentity
            }, completion: { finished in transitionContext.completeTransition(true)})
    }
    

这是最终结果的截屏视频:http://screencast.com/t/2mC07BLCC

我希望这能帮助您推进项目。

关于ios - 快速向 presentViewController 添加自定义转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798773/

有关ios - 快速向 presentViewController 添加自定义转换的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  4. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  5. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  6. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  7. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  10. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

随机推荐