触发时第一次模态segue慢

Posted

技术标签:

【中文标题】触发时第一次模态segue慢【英文标题】:modal segue slow first time when triggered 【发布时间】:2016-01-18 20:56:02 【问题描述】:

我已经为 viewController 创建了模态 segue,但是当我触发这个 segue 时,查看 viewController 时似乎有一些延迟。我在 viewController 中的所有内容都在 viewDidLoad 中。这种缓慢的模态转场是什么?

override func viewDidLoad() 
    super.viewDidLoad()

    self.title = "Caption".uppercaseString

    self.view.backgroundColor = UIColor(hexString: "#0B1E30")

    self.navigationController?.navigationBar.barTintColor = UIColor(gradientStyle: UIGradientStyle.LeftToRight, withFrame: CGRectMake(0, 0, self.view.frame.width, (self.navigationController?.navigationBar.frame.height)! + 20), andColors: [UIColor(hexString: "#7F5CE6"), UIColor(hexString: "#9D8FE2")])

    let closeButton = UIBarButtonItem(image: UIImage(named: "Cross"), style: UIBarButtonItemStyle.Done, target: self, action: "dismissController")
    self.navigationItem.leftBarButtonItem = closeButton

    textView?.text = "Add caption (optional)"
    textView?.becomeFirstResponder()
    textView?.keyboardAppearance = UIKeyboardAppearance.Dark
    textView?.font = UIFont(name: "Montserrat-Light", size: 13)
    textView?.textColor = UIColor(hexString: "#363636")

    capturedImageView = UIImageView(image: capturedPhoto)
    capturedImageView?.frame = CGRectMake(8,10, 85, 85)
    let path = UIBezierPath(rect: CGRectMake(8, 10, 85, 85))
    textView?.textContainer.exclusionPaths = [path]
    textView?.addSubview(capturedImageView!)

    facebookButton?.tag = 1
    facebookButton!.adjustsImageWhenHighlighted = false
    facebookButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
    facebookButton?.tintColor = UIColor.whiteColor()
    facebookButton?.backgroundColor = UIColor(hexString: "#395798")
    facebookButton?.contentMode = UIViewContentMode.Center
    facebookButton?.setTitle("", forState: UIControlState.Normal)

    let fBorder = CALayer()
    let width = CGFloat(1.0)
    fBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
    fBorder.frame = CGRect(x: 0, y: facebookButton!.frame.size.height - width, width:  facebookButton!.frame.size.width, height: facebookButton!.frame.size.height)

    fBorder.borderWidth = width
    facebookButton!.layer.addSublayer(fBorder)
    facebookButton!.layer.masksToBounds = true


    twitterButton?.tag = 2
    twitterButton!.adjustsImageWhenHighlighted = false
    twitterButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
    twitterButton?.contentMode = UIViewContentMode.Center
    twitterButton?.tintColor = UIColor.whiteColor()
    twitterButton?.setTitle("", forState: UIControlState.Normal)

    let tBorder = CALayer()
    tBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
    tBorder.frame = CGRect(x: 0, y: twitterButton!.frame.size.height - width, width:  twitterButton!.frame.size.width, height: twitterButton!.frame.size.height)
    tBorder.borderWidth = width
    twitterButton!.layer.addSublayer(tBorder)
    twitterButton!.layer.masksToBounds = true

    let tRightBorder = CALayer()
    tRightBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
    tRightBorder.frame = CGRect(x: 0, y: 0, width:  width, height: twitterButton!.frame.size.height)

    tRightBorder.borderWidth = width
    twitterButton!.layer.addSublayer(tRightBorder)

    instagramButton?.tag = 3
    instagramButton!.adjustsImageWhenHighlighted = false
    instagramButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
    instagramButton?.contentMode = UIViewContentMode.Center
    instagramButton?.tintColor = UIColor.whiteColor()
    instagramButton?.setTitle("", forState: UIControlState.Normal)

    let iBorder = CALayer()
         iBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
    iBorder.frame = CGRect(x: 0, y: instagramButton!.frame.size.height - width, width:  instagramButton!.frame.size.width, height: instagramButton!.frame.size.height)

    iBorder.borderWidth = width
    instagramButton!.layer.addSublayer(iBorder)
    instagramButton!.layer.masksToBounds = true

    let iLeftBorder = CALayer()
    iLeftBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
    iLeftBorder.frame = CGRect(x: 0, y: 0, width:  width, height: instagramButton!.frame.size.height)

    iLeftBorder.borderWidth = width
    instagramButton!.layer.addSublayer(iLeftBorder)


【问题讨论】:

将所有代码移到 viewWillApear 方法中。 确实没有帮助,顺便说一下,我正在我的 iphone 上进行测试。 你为什么不使用故事板来设计你的视图? 我不能使用情节提要添加图像和 caLayer? 查看***.com/a/28509716/3535399 【参考方案1】:

这让我感到意外,但当 textView 的属性“selectable”默认设置为 true(在 Storyboard 上选中)时,显然存在性能问题。

尝试转到您的 Storyboard,选择每个 textView 并取消选中“可选择”框。如果您需要 textView 是可选的,那么只需在 viewDidLoad 上以编程方式将 selectable 设置为 true。

【讨论】:

以上是关于触发时第一次模态segue慢的主要内容,如果未能解决你的问题,请参考以下文章

如何在触发退出 segue 之前执行操作?

从 Unwind segue 触发时,Push segue 不起作用

Unwind segues 不适用于模态视图控制器

无论凭据是不是正确,都会触发 Segue

Xcode 11 Beta 5 - 模态触发器仅一次

撤消模态并同时显示 segue