![](/img/trans.png)
[英]How can I get the edges of a UIImageView to snap to the edges of the screen (superview)?
[英]How do I determine the superview of UIImageView in iOS
我想確定哪個視圖包含iOS 7中用戶點擊的圖像。下面是我的布局。
+-------------------------------+
| A |
| +---------------------------+ |
| |B | |
| |+--+ +--+| |
| ||C | ------------------|D || |
| |+--+ +--+| |
| | | | | |
| | | G | | |
| | | | | |
| |+--+ +--+| |
| ||E | ------------------|F || |
| |+--+ +--+| |
| +---------------------------+ |
+-------------------------------+
Legend:
A - UIView
B - UIView subview of A
C, D, E, F - UIImageView subviews of B
G - UIImageView subview of A
我做了整個窗口層次結構的轉儲,它正確地顯示了C,D,E,F作為B和B的子視圖,G作為A的子視圖。這整個視圖層次結構在IB中構建並作為XIB加載。
在用戶點擊UIImageView C的情況下,我想找出它的超級視圖B.但是,當我在C類的hittest內部進行self.superview時,它總是為零。
如何確定UIImageView(C,D,E,F)superview?
如果您想知道,我正在嘗試創建夾點(C,D,E,F)來操縱G中的圖像。我一次在屏幕上有多個A實例,需要知道哪一個是被輕拍的握把的超視圖。
我在這里錯過了一些概念並欣賞你的指導。
更新:這是視圖的IB層次結構:
這是實際的xib:
這是C類hittest斷點處的視圖轉儲:
UIWindow:<UIWindow: 0xb174590
| UILayoutContainerView:<UILayoutContainerView: 0xb17a7f0
| | UINavigationTransitionView:<UINavigationTransitionView: 0xb17c160
| | | UIViewControllerWrapperView:<UIViewControllerWrapperView: 0xb18f640
| | | | CanvasBackgroundView:<CanvasBackgroundView: 0xa63be10
| | | | | DragView:<DragView: 0xa6aec60
| | | | | | BorderView:<BorderView: 0xa6b92c0
| | | | | | | GripView:<GripView: 0xa6b2480
| | | | | | | GripView:<GripView: 0xa6bb020
| | | | | | | GripView:<GripView: 0xa6b29e0
| | | | | | | GripView:<GripView: 0xa6bb0d0
| | | | | | ContentImageView:<ContentImageView: 0xa6bb340
@Timothy,我所有的hitTest方法都是這樣的:
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
DDLogVerbose(@"%@ superview: %@", NSStringFromClass([self class]), NSStringFromClass([[self superview] class]));
UIView *hitView = [super hitTest:point withEvent:event];
if (self == hitView) {
DDLogVerbose(@"hit for me: %@ %@ %@ self:%@", NSStringFromCGPoint(point), NSStringFromClass([hitView class]), NSStringFromCGRect(hitView.frame), NSStringFromCGRect(self.frame));
return self; // we handled it
} else {
DDLogVerbose(@"hit not for me: %@", NSStringFromCGPoint(point));
return nil; // not for us
}
}
嘗試:
UIView *iv = [imageView superView];
self.superview應該這樣做。 一個提示 - 當你在控制台中做'自我'時,你確定自己是imageView嗎? 嘗試分配ImageView的標記屬性,然后在調試時確保您看到相同的標記。 所有UIView都有標簽屬性。
旁注,視圖的超視圖,如果它在屏幕上可見。 另一個提示 - 你在哪里/什么時候拋出這個斷點來停止執行? 如果它在C類的init / build方法中某處,則可能尚未分配超級視圖。 確保在屏幕上顯示后查詢視圖。 你提到一些關於觸摸的事情 - 在tapGestureRecoginizer被擊中之后調用BP。
將UITapGestureRecognizer添加到GripViews並在點擊時調用代理。 確保將userInteractionEnabled屬性設置為YES。
如果您嘗試從主線程以外的任何其他線程獲取UIImageView的超級視圖,則返回nil。 此外,center屬性將包含垃圾值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.