将触摸传递给下一个响应者或其他子视图iOS
问题描述:
我相信这个问题很容易解决,但我对iOS开发相对较新。我正在尝试将传递的触摸事件处理为UIView上绘制顺序较低的子项。例如 -将触摸传递给下一个响应者或其他子视图iOS
我创建扩展的UIImageView来创建我的MoveableImage类。这个类只是基本的UIImageView一个实现的touchesBegan,touchesEnded和touchesMoved-
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self showFrame];
//if multitouch dont move
if([[event allTouches]count] > 1)
{
return;
}
UITouch *touch = [[event touchesForView:self] anyObject ];
// Animate the first touch
CGPoint colorPoint = [touch locationInView:self];
CGPoint touchPoint = [touch locationInView:self.superview];
//if color is alpha of 0 , they are touching the frame and bubble to next responder
UIColor *color = [self colorOfPoint:colorPoint];
[color getRed:NULL green:NULL blue:NULL alpha:&touchBeganAlpha];
NSLog(@"alpha : %f",touchBeganAlpha);
if(touchBeganAlpha > 0)
{
[self animateFirstTouchAtPoint:touchPoint];
}
else {
[super.nextResponder touchesBegan:touches withEvent:event];
}
}
所以最终的结果基本上是这个 - 如果他们接触的ImageView的其他图像,这将是内部的框架,而不是图像在下面可能会有所反应。看到这个图像的例子。
到目前为止,我已经尝试下一个响应者但这并不解决问题。任何帮助将不胜感激!
已解决 - 我停止检查touchesBegan和touchesMoved上的alpha。开发点InsideInside允许UIView为我处理。
-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *) event
{
BOOL superResult = [super pointInside:point withEvent:event];
if(!superResult)
{
return superResult;
}
if(CGPointEqualToPoint(point, self.previousTouchPoint))
{
return self.previousTouchHitTestResponse;
}else{
self.previousTouchPoint = point;
}
BOOL response = NO;
//if image is nil then return yes and fall back to super
if(self.image == nil)
{
response = YES;
}
response = [self isAlphaVisibleAtPoint:point];
self.previousTouchHitTestResponse = response;
return response;
}
答
您可以替代您的UIImageView子类的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
方法。 (这是一个uiview的方法,每个子类都可以覆盖)
UIView在hitTest:withEvent:
中使用此方法来确定哪个子视图应接收触摸事件。如果pointInside:withEvent:返回YES,则遍历子视图的层次结构;否则,视图层次结构的分支将被忽略。
检查OBShapedButton的github上的源代码。他们只处理按钮的不透明部分的轻敲事件。
工作非常感谢的人! – thebringking 2012-08-14 16:19:35