只有一个UIView子视图出现了吗?

时间:2015-12-22 16:33:57

标签: ios arrays swift uiview

我希望在UIView的每个角落都有一个角selected shape,在每个边缘的中间,一个在中心。

selected shape附加了UITapGestureRecognizer,但当我点击选定的形状时,只会放置最后一个角落the bottom right corner bRCorner, is what shows up

class DrawViewController: UIViewController {
     var selectedShape: UIView?
     var tLCorner: UIView?
     var tMCorner: UIView?
     var tRCorner: UIView?
     var mLCorner: UIView?
     var mMCorner: UIView?
     var mRCorner: UIView?
     var bLCorner: UIView?
     var bMCorner: UIView?
     var bRCorner: UIView?

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

    //for each corner: set color, add pan feature, place in view
    var corners = [UIView](count: 9, repeatedValue: UIView(frame: CGRectMake(0, 0, 10, 10)))
    for corner in corners{
        corner.backgroundColor = fontColor
        let pan = UIPanGestureRecognizer(target: self, action: "moveShape:")
        corner.addGestureRecognizer(pan)
        self.view.insertSubview(corner, aboveSubview: selectedShape!)
    }

    //place each corner in proper place
    tLCorner = corners[0]
    tLCorner!.center.x = (selectedShape?.frame.minX)!
    tLCorner!.center.y = (selectedShape?.frame.minY)!

    tMCorner = corners[1]
    tMCorner!.center.x = (selectedShape?.frame.midX)!
    tMCorner!.center.y = (selectedShape?.frame.minY)!

    tRCorner = corners[2]
    tRCorner!.center.x = (selectedShape?.frame.maxX)!
    tRCorner!.center.y = (selectedShape?.frame.minY)!

    mLCorner = corners[3]
    mLCorner!.center.x = (selectedShape?.frame.minX)!
    mLCorner!.center.y = (selectedShape?.frame.midY)!

    mMCorner = corners[4]
    mMCorner!.center = (selectedShape?.center)!

    mRCorner = corners[5]
    mRCorner!.center.x = (selectedShape?.frame.maxX)!
    mRCorner!.center.y = (selectedShape?.frame.midY)!

    bLCorner = corners[6]
    bLCorner!.center.x = (selectedShape?.frame.minX)!
    bLCorner!.center.y = (selectedShape?.frame.maxY)!

    bMCorner = corners[7]
    bMCorner!.center.x = (selectedShape?.frame.midX)!
    bMCorner!.center.y = (selectedShape?.frame.maxY)!

    bRCorner = corners[8] //the only corner that shows up!
    bRCorner!.center.x = (selectedShape?.frame.maxX)!
    bRCorner!.center.y = (selectedShape?.frame.maxY)!
}

然而,当我评论最后两行时:

//bRCorner!.center.x = (selectedShape?.frame.maxX)!
//bRCorner!.center.y = (selectedShape?.frame.maxY)!

要放置的最后一个角落成为底部中间角bMCornerbMCorner is what shows up

bMCorner = corners[7] //this is what shows up!
bMCorner!.center.x = (selectedShape?.frame.midX)!
bMCorner!.center.y = (selectedShape?.frame.maxY)!

我很困惑为什么会这样。我宁愿让所有的角落出现。任何有关这方面的帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

var corners = [UIView](count: 9, repeatedValue: UIView(frame: CGRectMake(0, 0, 10, 10)))

该代码创建一个大小为9的数组,其中包含UIView个。但这些观点都是完全相同的对象。基本上你只有一个UIView有9个引用它。而是在循环中移动创建:

var corners = [UIView]()
for var i in 1...9 {
    let view = UIView(frame: CGRectMake(0, 0, 10, 10))
    corners.append(view)
    // your customization
}

当您将以下代码置于其后时,您可以轻松看到差异:
 corners.forEach { print($0) }

为您的代码打印

  

<UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>> <UIView: 0x7fbc2a417100; frame = (0 0; 10 10); layer = <CALayer: 0x7fbc2a419ce0>>

=&GT; 相同内存位置/视图的9倍。

我的代码打印

  

<UIView: 0x7f825b420e60; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b4210e0>> <UIView: 0x7f825b42a6c0; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b401a80>> <UIView: 0x7f825b612420; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b611180>> <UIView: 0x7f825b50b960; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b504070>> <UIView: 0x7f825b50a250; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b611f30>> <UIView: 0x7f825b511e30; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b50b3e0>> <UIView: 0x7f825b725110; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b725280>> <UIView: 0x7f825b613850; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b613500>> <UIView: 0x7f825b727000; frame = (0 0; 10 10); layer = <CALayer: 0x7f825b724630>>

=&GT; 9 不同的观点。

苹果医生说

  

Swift的Array类型还提供了一个初始化器,用于创建一个特定大小的数组,所有值设置为相同的默认值。您将此初始值设定项传递给要添加到新数组的项目数(称为计数)和相应类型的默认值(称为repeatedValue):

粗体部分是您的确切问题。