UIImageView没有隐藏?

时间:2015-12-23 20:02:45

标签: ios swift reference uiimageview

我有一个selectedShape,一个正方形UIView,我正在通过移动corners之一UIImageView来改变其大小。 corners位于selectedShape的每个角落,位于selectedShape每个边缘的中间位置,位于selectedShape的中心。

当我通过移动selectedShape更改corner的大小时,我正在尝试隐藏未移动的其他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)
            }
        }
    }

}

我不确定发生了什么,但我觉得这与引用有关,因为当我在corner下的代码部分中将c更改为//set panned corner to panIcon时...

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

...正在移动的corner在移动时不再更改其图标。

如何解决这个问题的任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我现在想出了我的问题,就像@Caleb提到的那样。

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