我有一个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不再在移动时更改其图标。
如果您能帮忙解决这个问题,我们将不胜感激!

最佳答案

我已经解决了@Caleb提到的问题。
我需要使用赋值运算符=而不是=。c.hidden=true和c.hidden=false

关于ios - UIImageView没有隐藏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34442896/

10-09 08:05