开发者

Creating a bounding box for a View

开发者 https://www.devze.com 2023-02-21 22:55 出处:网络
Is there a way to a bounding box for a view so that its subviews cannot leave the view? I currently have UIImageViews which I move around, scale, rotate etc and they are able to leave the view area.

Is there a way to a bounding box for a view so that its subviews cannot leave the view?

I currently have UIImageViews which I move around, scale, rotate etc and they are able to leave the view area.

How does one set the superview to bound/hold the subviews within it?

How else can this be done. I currently detect the origin of the image this works to the point that the image moves until it reaches this origin, when it does, the image is stuck.

I use gestureRecognizers and this origin technique only works for panning/moving开发者_开发技巧 an image.

Any suggestions?


If you want to stop a view's subviews from being drawn outside its borders, you need to set its clipsToBounds property to YES. (See the UIView class reference for details.)

If you want to stop your views from being in certain positions, don't move them there! It's your code that's putting them where you don't want them to be. If you're using a gesture recogniser, presumably you have a method that responds to gestures by adjusting the frame of a view - put some conditions on this movement that prevent it from happening when you don't want it.

When you write these conditions, bear in mind that a view's origin is relative to its superview. For example, say you have a view controller with a view that takes up the whole screen of the device, and inside that a box that starts at 0, 100, and inside that some squares and circles and squiggly shapes that the user can move around. If you examine containerBox.bounds.origin, you'll find that it's 0, 100, but if you want to put a square in the top left corner of the box, you need to get its frame and set the origin to 0, 0. Something to look out for.

I didn't follow your explanation of the 'origin technique'. If you paste your code, I might be able to help.

0

精彩评论

暂无评论...
验证码 换一张
取 消