我确信这个问题会很容易解决,但是我对 iOS 开发还比较陌生。我正在尝试将触摸事件传递给 UIView 上绘制顺序较低的孩子。例如 -
我创建扩展 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。Ovveriding pointInside 允许 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;
}