iphone - 限制父 View 内的 View

标签 iphone objective-c ipad ios

我正在尝试使用 touches moved 方法将一个 UIView - “A”( subview )移动到另一个 UIView - “B”(SuperView)中。我能够将 UIView 移到 super View 之外。我想将 UIView 限制在 Superview 范围内。有什么方法可以有一个通用方法来测试 subview 是否在父 View 的可见矩形内???

最佳答案

听起来您想限制 subview (viewA) 的移动始终完全包含在父 View (viewB) 中。 CGRectContainsRect 是正确的答案,但必须谨慎应用,因为 subview 框架是在其父 View 的坐标系中指定的。

// inside touches moved, compute the newViewAFrame based on the movement
// but only assign it if it meets the containment constraint:

if (CGRectContainsRect(viewB.bounds, newViewAFrame)) {
    viewA.frame = newViewAFrame;
}

请注意,我们没有在检查中提及 viewB.frame。 viewB 在其父级中的位置与 viewB 是否包含 viewA 无关。

关于iphone - 限制父 View 内的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11896245/

相关文章:

iphone - 在 Objective-C 和 iOS 中合并项目范围定义的最佳实践

iphone - 从 iPhone 中的“我的应用程序”打开 Native Map 应用程序

ios - 自动调整 tableViewCell 中的 textView - iOS 9 (Objective-C)

ios - UITableView 滚动条没有拉伸(stretch)

ios - iPad 中的方向框架不改变

iphone - apple-mobile-web-app-capable 和 apple-touch-fullscreen 之间的区别 | iPhone/iOS 元标签

ios - 应用程序在 iOS 中显示黑屏

objective-c - 保持数组排序 - 在设置时、获取时还是稍后?

ios - iOS 7 中的自定义 UIView 方向问题

iPhone和iPad,播放视频时截图