2015-09-14 39 views
0

在下面的RayW自动布局挑战6中,我无法理解代码中的哪个位置,宽度和高度在多次点击后会发生变化。了解设置自动约束条件的位置

它在IB中设置为50,并且在tappedImage方法中的第一次运行中,在最后一个条件中,width.constant = 100,但是何时将其重新定义回50(width.constant = 50)?!?

并且,在第一次点击(即,该图像的宽度和高度= 100)之后,再次点击另一图像,第一图像的宽度和高度回到50,而第二图像增长到100。最后的其他条件是否告诉第一个图像调整到50?

import UIKit 

    class ViewController: UIViewController { 

    @IBOutlet weak var rayWidth: NSLayoutConstraint! 
    @IBOutlet weak var rayHeight: NSLayoutConstraint! 
    @IBOutlet weak var ray: UIImageView! 

    @IBOutlet weak var vickiWidth: NSLayoutConstraint! 
    @IBOutlet weak var vickiHeight: NSLayoutConstraint! 
    @IBOutlet weak var vicki: UIImageView! 

    @IBOutlet weak var gregWidth: NSLayoutConstraint! 
    @IBOutlet weak var gregHeight: NSLayoutConstraint! 
    @IBOutlet weak var greg: UIImageView! 

    @IBOutlet weak var micWidth: NSLayoutConstraint! 
    @IBOutlet weak var micHeight: NSLayoutConstraint! 
    @IBOutlet weak var mic: UIImageView! 

    @IBOutlet weak var christineWidth: NSLayoutConstraint! 
    @IBOutlet weak var christineHeight: NSLayoutConstraint! 
    @IBOutlet weak var christine: UIImageView! 

    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var bio: UITextView! 

    var heights:[NSLayoutConstraint]! 
    var widths:[NSLayoutConstraint]! 
    var bios: [String]! 
    var names: [String]! 


    @IBOutlet weak var emailButton: UIButton! 
    var previousHeight: NSLayoutConstraint? 
    var previousWidth: NSLayoutConstraint? 

    override func viewWillAppear(animated: Bool) { 

    bios = ["Ray is an indie software developer currently focusing on iPhone and iPad development, and the administrator of this site. He’s the founder of a small iPhone development studio called Razeware, and is passionate both about making apps and teaching others the techniques to make them.", "Vicki Wenderlich discovered a love of digital art in 2009, and has been making app art and digital illustrations ever since. She is passionate about helping people pursue their dreams, and makes free app art for developers available on her website, http://www.vickiwenderlich.com.", "Greg is an iOS developer and trainer, and has been on the raywenderlich.com editorial team since 2012. He has been nerding out with computers since the Commodore 64 era in the 80s and continues to this day on the web and on iOS. He likes caffeine, codes with two-space tabs, and writes with semicolons.", "Mic Pringle is a developer, editor, podcaster, and video tutorial maker. He's also Razeware's third full-time employee. When not knee-deep in Swift or stood in-front of his green screen, he enjoys spending time with his wife Lucy and their daughter Evie, as-well as attending the football matches of his beloved Fulham FC. You can find Mic on Twitter, GitHub, and Stack Overflow.", "Christine is Ray's administrative assistant. She tries to keep order in the ever expanding world of raywenderlich.com so that Ray and the team can stay focused on making more tutorials, books, and apps!"] 
    names = ["Ray Wenderlich", "Vicki Wenderlich", "Greg Heo", "Mic Pringle", "Christine Sweigart"] 
    name.text = "" 
    emailButton.hidden = true 

    } 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    var razeware = [ray, vicki, greg, mic, christine] 
    heights = [rayHeight, vickiHeight, gregHeight,micHeight,christineHeight] 
    widths = [rayWidth, vickiWidth, gregWidth,micWidth,christineWidth] 


    for image in razeware { 
     var tapGesture = UITapGestureRecognizer(target: self, action: "tappedImage:") 
     image.userInteractionEnabled = true 
     image.addGestureRecognizer(tapGesture) 
    } 
    } 

    func tappedImage(sender:UITapGestureRecognizer!) { 

    var tag = 0 
    // index/tag who's been tapped 
    if let senderTag = sender.view?.tag { 
     tag = senderTag 
    } 

    let width = widths[tag]    // get the width constraint from the array 
    let height = heights[tag] 

    print(width) 

    view.setNeedsUpdateConstraints() 

    if previousHeight == height { 
     if previousHeight?.constant == 100 { 
     previousHeight?.constant = 50 
     previousWidth?.constant = 50 
     print("through here") 
     //UIView.animateWithDuration(3.0, animations: {() -> Void in 
      self.name.text = "" 
      self.bio.text = "" 
      self.emailButton.hidden = true 
      // self.view.layoutIfNeeded() 
    // }) 
     } else { 
     previousHeight?.constant = 100 
     previousWidth?.constant = 100 
     print("Going here") 
     name.text = names[tag] 
     bio.text = bios[tag] 
     bio.font = UIFont.systemFontOfSize(15.0) 
     bio.textColor = UIColor.whiteColor() 
     emailButton.hidden = false 
     } 
    } else { 
     previousHeight?.constant = 50 
     previousWidth?.constant = 50 
      UIView.animateWithDuration(5.0, animations: {() -> Void in 
      width.constant = 100 
      height.constant = 100 
      self.name.text = self.names[tag] 
      self.bio.text = self.bios[tag] 
      self.bio.font = UIFont.systemFontOfSize(15.0) 
      self.bio.textColor = UIColor.whiteColor() 
      self.emailButton.hidden = false 
      self.view.layoutIfNeeded() 
     }) 
    } 
    print(width) 

    previousHeight = height 
    previousWidth = width 

    print("Previous: \(previousHeight)") 
    print("Height: \(height)") 
    } 

    override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
    } 


    } 
+0

是否layoutIfNeeded()求助于在IB中定义的约束值,除非在setNeedsUpdateContraints()之后定义了另一个值?即使约束值在代码中被重新定义? – DrWhat

回答

0

即使限制在代码重新定义别处之前,layoutIfNeeded()将恢复到原来的IB定义的约束值 - 除非约束再次setNeedsUpdateContraints之后重新定义(保持其新值) ()。

这是基于大量试用更多的错误。也许仍然有更好的答案...