2015-12-23 67 views
1

我有一個selectedShape,一個方形UIView,我正在通過移動其中一個corners,UIImageView來更改其大小。 corners被放置在selectedShape的每個角落,在selectedShape的每個邊緣的中間,並且在selectedShape的中心。UIImageView沒有隱藏?

雖然我通過移動corner更改了selectedShape的大小,但我試圖隱藏未被移動的其他corners。但是,這不會發生爲you can see here

class DrawViewController: UIViewController { 
    var corners: [UIImageView] = [UIImageView]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //set up corners: set color, add pan feature, count = 9 
     for i in 0...8 { 
      let corner = UIImageView(frame: CGRectMake(0, 0, 10, 10)) 
      corner.backgroundColor = fontColor 
      corners.append(corner) 
      corner.tag = i 
      corner.userInteractionEnabled = true 
      let pan = UIPanGestureRecognizer(target: self, action: "moveShape:") 
      corner.addGestureRecognizer(pan) 
    } 

    func tapShape(sender: UITapGestureRecognizer){ 
     selectedShape = sender.view! 

     //place each corner in view 
     for corner in corners { 
      self.view.insertSubview(corner, aboveSubview: selectedShape!) 
     } 

     //place each corner in proper place 
     corners = placeCorners(corners, selectedShape: selectedShape!) 
    } 

    func placeCorners(corners: [UIImageView], selectedShape: UIView)-> [UIImageView]{ 
     //place the diagonal corners 
     corners[0].center.x = selectedShape.frame.minX 
     corners[0].center.y = selectedShape.frame.minY 
     corners[2].center.x = selectedShape.frame.maxX 
     corners[2].center.y = selectedShape.frame.minY 
     corners[6].center.x = selectedShape.frame.minX 
     corners[6].center.y = selectedShape.frame.maxY 
     corners[4].center.x = selectedShape.frame.maxX 
     corners[4].center.y = selectedShape.frame.maxY 

     //place the horizontal corners 
     corners[1].center.x = selectedShape.frame.minX 
     corners[1].center.y = selectedShape.frame.midY 
     corners[3].center.x = selectedShape.frame.maxX 
     corners[3].center.y = selectedShape.frame.midY 

     //place the vertical corners 
     corners[5].center.x = selectedShape.frame.midX 
     corners[5].center.y = selectedShape.frame.minY 
     corners[7].center.x = selectedShape.frame.midX 
     corners[7].center.y = selectedShape.frame.maxY 

     //place the center corner 
     corners[8].center = selectedShape.center 

     return corners 
    } 

    func moveShape(sender: UIPanGestureRecognizer){ 
     let corner = sender.view as! UIImageView 

     //resize shape 
     let trans = sender.translationInView(canvas) 
     corner.center = CGPoint(x: corner.center.x + trans.x, y: corner.center.y + trans.y) 
     if let shape = selectedShape{ 
      shape.frame.size = CGSize(width: shape.frame.width + trans.x , height: shape.frame.height + trans.y) 
     } 
     sender.setTranslation(CGPointZero, inView: canvas) 

     //hide corners while changing size 
     if (sender.state == UIGestureRecognizerState.Began || sender.state == UIGestureRecognizerState.Changed){ 
      for c in corners{ 
       //set panned corner to panIcon 
       if c.tag == corner.tag{ 
        corner.backgroundColor = UIColor.clearColor() 
        corner.image = UIImage(named: "panIcon") 
        corner.contentMode = UIViewContentMode.ScaleAspectFit 
       } 
       c.hidden == true 
      } 
     } 

     //unhide corners when finish changing size 
     if(sender.state == UIGestureRecognizerState.Ended || sender.state == UIGestureRecognizerState.Failed || sender.state == UIGestureRecognizerState.Cancelled){ 
      for c in corners{ 
       //set panned corner back to grey square 
       if c.tag == corner.tag{ 
        corner.backgroundColor = fontColor 
        corner.image = nil 
       } 
       c.hidden == false 
      } 
      if let shape = selectedShape{ 
       corners = placeCorners(corners, selectedShape: shape) 
      } 
     } 
    } 

} 

我不知道這是怎麼回事,但我有一種感覺,這與參考文獻做,因爲當我在//set panned corner to panIcon下的部分代碼更改cornerc ...

//set panned corner to panIcon 
if c.tag == corner.tag{ 
    c.backgroundColor = UIColor.clearColor() 
    c.image = UIImage(named: "panIcon") 
    c.contentMode = UIViewContentMode.ScaleAspectFit 
} 

...正在移動的corner不再在移動時更改其圖標。

任何有關如何解決這個問題的幫助將不勝感激!

+4

您需要使用賦值運算符'='而不是'=='。 'c.hidden = true'和'c.hidden = false' – Caleb

+0

ohhhh正確的感謝@Caleb指出這一點! – 14wml

回答

0

我現在已經想出了我的問題,因爲@Caleb提到。

我需要使用賦值運算符=而不是==。 c.hidden = true和c.hidden = false