mle*_*evi 14 iphone objective-c uiview ios swift
我有一个pan手势的视图和一个UIPushBehavior连接到它想知道它是否可以检查视图何时超出了超视图边界.基本上用户扔视图,我想在视图不在屏幕时运行一些动画.无法弄清楚如何做到这一点.谢谢.
Ste*_*son 17
如果你想检查它是否完全超出了它的超视范围,你就可以做到这一点
if (!CGRectContainsRect(view.superview.bounds, view.frame))
{
//view is completely out of bounds of its super view.
}
Run Code Online (Sandbox Code Playgroud)
如果你想检查它的一部分是否超出范围你可以做
if (!CGRectEqualToRect(CGRectIntersection(view.superview.bounds, view.frame), view.frame))
{
//view is partially out of bounds
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,菲利普对部分越界检查的回答在这一行中并不完全正确:
v1.bounds.intersection(v2.frame).width > 0) && (v1.bounds.intersection(v2.frame).height > 0
交叉点大小可以大于零,并且视图仍然位于超视图边界内。
事实证明,equal(to: CGRect)由于 CGFloat 的准确性,我无法安全使用。
这是更正的版本:
func outOfSuperviewBounds() -> Bool {
guard let superview = self.superview else {
return true
}
let intersectedFrame = superview.bounds.intersection(self.frame)
let isInBounds = fabs(intersectedFrame.origin.x - self.frame.origin.x) < 1 &&
fabs(intersectedFrame.origin.y - self.frame.origin.y) < 1 &&
fabs(intersectedFrame.size.width - self.frame.size.width) < 1 &&
fabs(intersectedFrame.size.height - self.frame.size.height) < 1
return !isInBounds
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6059 次 |
| 最近记录: |