【发布时间】:2012-09-21 10:37:12
【问题描述】:
我有一个大小为 (0,0,320,280) 的基本视图 A,它包含另一个大小为 (100,100,50,50) 和视图 B 有一个按钮和一个图像视图 (C) 作为子视图。图像视图框架与视图 B 相同,在 B 的左上角添加了按钮。
我的要求是当我们拖动B的右下角时,它的大小必须增加或减少。
如果我们从除右下角以外的任何其他位置拖动 B,它必须移动。不应修改视图大小。
我的问题是视图 B 没有收到触摸动作。
我添加了下面的代码。请指导我。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
//baseVw-------> view B//
if ([touch view]==baseVw)
{
touchStart = [[touches anyObject] locationInView:baseVw];
isResizingLR = (baseVw.bounds.size.width - touchStart.x < kResizeThumbSize && baseVw.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (baseVw.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && baseVw.bounds.size.height -touchStart.y <kResizeThumbSize);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [[touches anyObject] locationInView:baseVw];
CGPoint previous=[[touches anyObject]previousLocationInView:baseVw];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
if ([touch view]==baseVw)
{
if (isResizingLR)
{
baseVw.frame = CGRectMake(baseVw.frame.origin.x, baseVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
}
else
{
CGPoint activePoint = [[touches anyObject] locationInView:baseVw];
// Determine new point based on where the touch is now located
CGPoint newPoint = CGPointMake(baseVw.center.x + (activePoint.x - touchStart.x),
baseVw.center.y + (activePoint.y - touchStart.y));
//--------------------------------------------------------
// Make sure we stay within the bounds of the parent view
//--------------------------------------------------------
float midPointX = CGRectGetMidX(baseVw.bounds);
// If too far right...
if (newPoint.x > baseVw.superview.bounds.size.width - midPointX)
newPoint.x = baseVw.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(baseVw.bounds);
// If too far down...
if (newPoint.y > baseVw.superview.bounds.size.height - midPointY)
newPoint.y = baseVw.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY) // If too far up...
newPoint.y = midPointY;
// Set new center location
baseVw.center = newPoint;
}
}
}
【问题讨论】:
-
你的
UIImageView(即C)有userInteractionEnabled = YES吗? -
是的,我设置 userInteractionEnabled = YES
-
因此,如果您添加日志语句,则“B”视图永远不会被触及。为什么不让按钮成为“B”的子视图而不是 UIImageView - 它似乎更干净。我还假设“B”大于按钮和图像视图。 “B”有 userInteractionEnabled。
-
如果你看到这个我就会知道为什么我在 B 中添加了按钮。stackoverflow.com/questions/12525512/…
标签: ios uiview uiimageview touch superview