gpt4 book ai didi

ios - 只有一个 UIView subview 出现?

转载 作者:行者123 更新时间:2023-11-29 01:22:37 25 4
gpt4 key购买 nike

我希望在所选形状的每个角上都有一个角UIView,在每条边的中间,在中心有一个角。

选定的形状附加了一个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)!

我很困惑为什么会发生这种情况。我希望所有的角落都显示出来。如有任何帮助,我们将不胜感激!

最佳答案

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

该代码使用 UIView 创建了一个大小为 9 的数组在里面。但这些 View 都是完全相同的对象。你只有一个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>>

=> 9 次完全相同 内存位置/ View 。

对于我的代码,它打印

<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>>

=> 9 个不同 View 。

苹果文档说

Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue):

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

关于ios - 只有一个 UIView subview 出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34420407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com