如何向imageView添加UIview同时确保UIview的内容在它的框架内?

时间:2022-01-12 19:39:17

I have a superview which contains a ImageView, a testView, and some buttons. I created a UIView which contains ImageView2 and a removebutton. When I add UIView to UIImageView, the contents of UIView are not in the frame of UIView. Because of that, I cannot apply pan gesture recognizer. I have tried to apply constraints and to change the frame and centre of ImageView2.

我有一个包含ImageView、testView和一些按钮的超视图。我创建了一个包含ImageView2和removebutton的UIView。当我向UIImageView添加UIView时,UIView的内容不在UIView的框架内。正因为如此,我无法应用pan手势识别器。我尝试应用约束,并改变ImageView2的框架和中心。

Below is the code I am using to create the above mentioned UIView. How do I ensure that the contents of the UIView are visible in the frame of the UIView?

下面是我用来创建上面提到的UIView的代码。如何确保UIView的内容在UIView的框架中可见?

let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)

testview = UIView(frame: rect)
testview.layer.cornerRadius = testview.frame.width/2
testview.backgroundColor = UIColor.blackColor()
testview.clipsToBounds = true

imageView2 = UIImageView(frame: CGRect(x: testview.frame.minX+10, y: testview.frame.minY+10, width: 60, height: 60))

let rect2 = CGRect(x: self.testview.frame.maxX-17, y: self.testview.frame.minY, width: 20, height: 20)

removeButton = UIButton(frame: rect2)

removeButton.layer.cornerRadius = removeButton.frame.width/2

removeButton.backgroundColor = UIColor.blackColor()

removeButton.clipsToBounds = true

imageView2.layer.cornerRadius = imageView2.frame.width/2

imageView2.userInteractionEnabled = true

imageView2.clipsToBounds = true
testview.alpha = 0.3

imageView2.center = testview.center

imageView2.center = testview.center

testview.addSubview(imageView2)

testview.addSubview(removeButton)

testview.bringSubviewToFront(imageView2)

imageView.addSubview(testview)

1 个解决方案

#1


1  

The problem is that lines like this do not do what you think:

问题是,像这样的线条不能做你认为的:

let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)
// ... and ...
imageView2.center = testview.center

The center of a view is in its frame coordinates - it has to do with how the view is placed in its superview. But the position of a subview must be described in terms of its superviews bounds, not its frame. Its bounds are its internal coordinates, which is what you want.

视图的中心在它的帧坐标中——它与视图如何放在它的父视图中有关。但是子视图的位置必须根据父视图的边界而不是它的框架来描述。它的边界是它的内部坐标,这是你想要的。

Let's take the simplest case: how to center a subview in a superview. This is not the way:

让我们以最简单的情况为例:如何将子视图居中于父视图中。这不是方法:

imageView2.center = testview.center

You see the problem? testview.center is where testview is located in its superview; it is not the internal center of testview, which is what you want. The internal center of testview comes from its bounds. This is the way:

你看这个问题?testview。中心是testview位于父视图中的位置;它不是testview的内部中心,这正是您想要的。testview的内部中心来自它的边界。这是道:

let c = CGPointMake(testview.bounds.midX, testview.bounds.midY)
imageView2.center = c

#1


1  

The problem is that lines like this do not do what you think:

问题是,像这样的线条不能做你认为的:

let rect = CGRect(x: self.imageView.center.x-30, y: self.imageView.center.y-30, width: 80, height: 80)
// ... and ...
imageView2.center = testview.center

The center of a view is in its frame coordinates - it has to do with how the view is placed in its superview. But the position of a subview must be described in terms of its superviews bounds, not its frame. Its bounds are its internal coordinates, which is what you want.

视图的中心在它的帧坐标中——它与视图如何放在它的父视图中有关。但是子视图的位置必须根据父视图的边界而不是它的框架来描述。它的边界是它的内部坐标,这是你想要的。

Let's take the simplest case: how to center a subview in a superview. This is not the way:

让我们以最简单的情况为例:如何将子视图居中于父视图中。这不是方法:

imageView2.center = testview.center

You see the problem? testview.center is where testview is located in its superview; it is not the internal center of testview, which is what you want. The internal center of testview comes from its bounds. This is the way:

你看这个问题?testview。中心是testview位于父视图中的位置;它不是testview的内部中心,这正是您想要的。testview的内部中心来自它的边界。这是道:

let c = CGPointMake(testview.bounds.midX, testview.bounds.midY)
imageView2.center = c