Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将触摸传递到UIWebView的一部分_Objective C_Ios_Uiwebview - Fatal编程技术网

Objective c 将触摸传递到UIWebView的一部分

Objective c 将触摸传递到UIWebView的一部分,objective-c,ios,uiwebview,Objective C,Ios,Uiwebview,我有一个UIWebView。使用类似这样的方法: - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = (UITouch *)[touches anyObject]; CGPoint location = [touch locationInView:self]; //min_x,max_x,min_y,max_y are the min and max coord of you

我有一个UIWebView。使用类似这样的方法:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = (UITouch *)[touches anyObject];
CGPoint location = [touch locationInView:self];

//min_x,max_x,min_y,max_y are the min and max coord of your rect below

if (CGRectContainsPoint(CGRectMake(min_x,min_y,max_x,max_y),location))
{
     //send the touches to the view below
}

}

。。我已使UIWebView透明。我现在需要能够将web视图上的触摸传递到下面的视图,但只能传递到web视图的矩形部分

因此,假设webview是一个正方形,它的中心有一个正方形区域,必须通过触摸才能看到下面的视图


有办法做到这一点吗?

我的第一个想法是这样的:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = (UITouch *)[touches anyObject];
CGPoint location = [touch locationInView:self];

//min_x,max_x,min_y,max_y are the min and max coord of your rect below

if (CGRectContainsPoint(CGRectMake(min_x,min_y,max_x,max_y),location))
{
     //send the touches to the view below
}

}

如果下面的视图是静止的,但如果有按钮,或者它移动,你没有给出任何关于下面视图的信息,那么这应该是可行的。诸如此类的东西……是的。这样做吧

我的第一个想法是这样的:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = (UITouch *)[touches anyObject];
CGPoint location = [touch locationInView:self];

//min_x,max_x,min_y,max_y are the min and max coord of your rect below

if (CGRectContainsPoint(CGRectMake(min_x,min_y,max_x,max_y),location))
{
     //send the touches to the view below
}

}

如果下面的视图是静止的,但是如果有按钮,或者如果它移动,您还没有给出关于下面视图的任何信息,那么这应该是可行的。诸如此类……是的。这样做可以创建UIWebView的子类,并覆盖其hitTest:withEvent:和pointInside:withEvent:忽略“洞”-但是

或者,您可以创建UIView子类并将UIWebView作为子视图放置在其中,然后将其hitest:withEvent:method返回UIWebView下的视图

// - (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// This overrides the usual hitTest method to give priority to objects that are not in a chosen class.
// For this example, that class has been hard-coded to UIWebView.
// This allows the webview to be displayed over other interactive components.
// For the UIWebView case, it would be nice to look at its layout and only return views beneath it in locations where it is transparent, but that information is not obtainable.
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    UIView *viewFound = nil;
    Class lowPriorityClass = [UIWebView class];
    NSMutableArray *lowPriorityViews = [NSMutableArray array];

    // Search the subviews from front to back.
    NSEnumerator *viewEnumerator = [[self subviews] reverseObjectEnumerator];
    UIView *subview;
    while ((subview = [viewEnumerator nextObject]) && (!viewFound))
    {
        // Save low-priority cases for later.
        if ([subview isKindOfClass:lowPriorityClass])
            [lowPriorityViews addObject:subview];
        else
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    if (!viewFound)
    {
        // At this point, no other interactive views were found, so search the low-priority cases previously stored to see if they apply.
        // Since the lowPriorityViews are already in front to back order, enumerate forward.
        viewEnumerator = [lowPriorityViews objectEnumerator];
        while ((subview = [viewEnumerator nextObject]) && (!viewFound))
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    // All cases dealt with, return whatever was found.  This will be nil if no views were under the point.
    return viewFound;
}
以下是UIView子类的通用hitTest方法,该方法优先考虑任何非UIWebView的交互式子视图,即使它们位于UIWebView之下。它没有任何特定的“洞”检测,但hitTest也是您想要做的。在忽略UIWebView之前,只需确定点是否在孔中

// - (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// This overrides the usual hitTest method to give priority to objects that are not in a chosen class.
// For this example, that class has been hard-coded to UIWebView.
// This allows the webview to be displayed over other interactive components.
// For the UIWebView case, it would be nice to look at its layout and only return views beneath it in locations where it is transparent, but that information is not obtainable.
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    UIView *viewFound = nil;
    Class lowPriorityClass = [UIWebView class];
    NSMutableArray *lowPriorityViews = [NSMutableArray array];

    // Search the subviews from front to back.
    NSEnumerator *viewEnumerator = [[self subviews] reverseObjectEnumerator];
    UIView *subview;
    while ((subview = [viewEnumerator nextObject]) && (!viewFound))
    {
        // Save low-priority cases for later.
        if ([subview isKindOfClass:lowPriorityClass])
            [lowPriorityViews addObject:subview];
        else
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    if (!viewFound)
    {
        // At this point, no other interactive views were found, so search the low-priority cases previously stored to see if they apply.
        // Since the lowPriorityViews are already in front to back order, enumerate forward.
        viewEnumerator = [lowPriorityViews objectEnumerator];
        while ((subview = [viewEnumerator nextObject]) && (!viewFound))
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    // All cases dealt with, return whatever was found.  This will be nil if no views were under the point.
    return viewFound;
}

您可以创建UIWebView的子类,并覆盖其hitTest:withEvent:和pointInside:withEvent:以忽略“洞”-但是

或者,您可以创建UIView子类并将UIWebView作为子视图放置在其中,然后将其hitest:withEvent:method返回UIWebView下的视图

// - (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// This overrides the usual hitTest method to give priority to objects that are not in a chosen class.
// For this example, that class has been hard-coded to UIWebView.
// This allows the webview to be displayed over other interactive components.
// For the UIWebView case, it would be nice to look at its layout and only return views beneath it in locations where it is transparent, but that information is not obtainable.
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    UIView *viewFound = nil;
    Class lowPriorityClass = [UIWebView class];
    NSMutableArray *lowPriorityViews = [NSMutableArray array];

    // Search the subviews from front to back.
    NSEnumerator *viewEnumerator = [[self subviews] reverseObjectEnumerator];
    UIView *subview;
    while ((subview = [viewEnumerator nextObject]) && (!viewFound))
    {
        // Save low-priority cases for later.
        if ([subview isKindOfClass:lowPriorityClass])
            [lowPriorityViews addObject:subview];
        else
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    if (!viewFound)
    {
        // At this point, no other interactive views were found, so search the low-priority cases previously stored to see if they apply.
        // Since the lowPriorityViews are already in front to back order, enumerate forward.
        viewEnumerator = [lowPriorityViews objectEnumerator];
        while ((subview = [viewEnumerator nextObject]) && (!viewFound))
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    // All cases dealt with, return whatever was found.  This will be nil if no views were under the point.
    return viewFound;
}
以下是UIView子类的通用hitTest方法,该方法优先考虑任何非UIWebView的交互式子视图,即使它们位于UIWebView之下。它没有任何特定的“洞”检测,但hitTest也是您想要做的。在忽略UIWebView之前,只需确定点是否在孔中

// - (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
// This overrides the usual hitTest method to give priority to objects that are not in a chosen class.
// For this example, that class has been hard-coded to UIWebView.
// This allows the webview to be displayed over other interactive components.
// For the UIWebView case, it would be nice to look at its layout and only return views beneath it in locations where it is transparent, but that information is not obtainable.
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    UIView *viewFound = nil;
    Class lowPriorityClass = [UIWebView class];
    NSMutableArray *lowPriorityViews = [NSMutableArray array];

    // Search the subviews from front to back.
    NSEnumerator *viewEnumerator = [[self subviews] reverseObjectEnumerator];
    UIView *subview;
    while ((subview = [viewEnumerator nextObject]) && (!viewFound))
    {
        // Save low-priority cases for later.
        if ([subview isKindOfClass:lowPriorityClass])
            [lowPriorityViews addObject:subview];
        else
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    if (!viewFound)
    {
        // At this point, no other interactive views were found, so search the low-priority cases previously stored to see if they apply.
        // Since the lowPriorityViews are already in front to back order, enumerate forward.
        viewEnumerator = [lowPriorityViews objectEnumerator];
        while ((subview = [viewEnumerator nextObject]) && (!viewFound))
        {
            CGPoint pointInSubview = [self convertPoint:point toView:subview];
            if ([subview pointInside:pointInSubview withEvent:event])
            {
                // Subviews may themselves have subviews to return.
                viewFound = [subview hitTest:pointInSubview withEvent:event];
                // However, if not, return the subview itself.
                if (!viewFound)
                    viewFound = subview;
            }
        }
    }

    // All cases dealt with, return whatever was found.  This will be nil if no views were under the point.
    return viewFound;
}

我认为有一个非常简单的方法来解决你的问题。。。您没有指定它,但我推断您不希望用户与webview HTML交互。如果是这样,只需在webview上禁用UserInteractionEnabled,触摸就会通过

然后,您需要如您所述位于中心的活动视图


我希望它能帮助你,如果你需要任何澄清,就评论一下。

我认为有一个非常简单的方法来解决你的问题。。。您没有指定它,但我推断您不希望用户与webview HTML交互。如果是这样,只需在webview上禁用UserInteractionEnabled,触摸就会通过

然后,您需要如您所述位于中心的活动视图


我希望它能有所帮助,如果你需要任何澄清,请发表评论。

我还有另一个想法:获得全球点击。当有人点击iPhone时,它会向global sendEvent发送一条消息。所以你听它,做你想做的事情

要做到这一点,您需要对UIApplication进行子类化。首先,在main.c中使用此函数

    int retVal = UIApplicationMain(argc, argv, @"myUIApplicationClass" ,nil);
然后实现这个类:

@implementation BBAplicationDebug

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    NSSet *touches = [event allTouches];
    UITouch *touch = touches.anyObject;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"userClick" object:touch];
}

@end

现在您可以收听此NSNotification并执行所有检查:是否在正确的帧中,是否具有正确的UIView,等等。

我有另一个想法:获得全局单击。当有人点击iPhone时,它会向global sendEvent发送一条消息。所以你听它,做你想做的事情

要做到这一点,您需要对UIApplication进行子类化。首先,在main.c中使用此函数

    int retVal = UIApplicationMain(argc, argv, @"myUIApplicationClass" ,nil);
然后实现这个类:

@implementation BBAplicationDebug

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    NSSet *touches = [event allTouches];
    UITouch *touch = touches.anyObject;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"userClick" object:touch];
}

@end

现在您可以收听此NSNotification并执行所有检查:是否在正确的帧中,是否具有正确的UIView,等等。

这是一种更具体的hitTest操作形式

首先,创建UIView的子类。我把我的观点称为“有洞”

在其标题中,为UIView定义一个属性,该UIView将是通过外部视图的孔。让这成为一个出口

然后覆盖hitTest。如果该点在孔内返回命中,则返回该点。否则,返回它通常会返回的内容

- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    CGPoint pointInHole = [self convertPoint:point toView:self.holeView];
    UIView *viewInHole = [self.holeView hitTest:pointInHole withEvent:event];

    if (viewInHole)
        return viewInHole;
    else
        return [super hitTest:point withEvent:event];
}
在Interface Builder中,或以编程方式放置带有孔的视图。按顺序在其中放置UIView和UIWebView。使UIView成为ViewWithHole的孔视图

holeView本身可以是交互式的,也可以在其中放置任意数量的交互式视图。在这里,我在那里放了一些标准视图,以确保它们工作正常。孔内的任何点击都将发送到孔及其子视图,而不是顶部的UIWebView。如往常一样,UIWebView将接收holeView之外的任何点击


可以在孔前面显示任意数量和类型的视图;重要的是holeView连接正确。

这是一种更具体的hitTest形式 操纵

首先,创建UIView的子类。我把我的观点称为“有洞”

在其标题中,为UIView定义一个属性,该UIView将是通过外部视图的孔。让这成为一个出口

然后覆盖hitTest。如果该点在孔内返回命中,则返回该点。否则,返回它通常会返回的内容

- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event;
{
    CGPoint pointInHole = [self convertPoint:point toView:self.holeView];
    UIView *viewInHole = [self.holeView hitTest:pointInHole withEvent:event];

    if (viewInHole)
        return viewInHole;
    else
        return [super hitTest:point withEvent:event];
}
在Interface Builder中,或以编程方式放置带有孔的视图。按顺序在其中放置UIView和UIWebView。使UIView成为ViewWithHole的孔视图

holeView本身可以是交互式的,也可以在其中放置任意数量的交互式视图。在这里,我在那里放了一些标准视图,以确保它们工作正常。孔内的任何点击都将发送到孔及其子视图,而不是顶部的UIWebView。如往常一样,UIWebView将接收holeView之外的任何点击


可以在孔前面显示任意数量和类型的视图;重要的是holeView连接正确。

你能提供一张你想做什么的图片吗?特别是,UIWebView中的漏洞是固定在后面的视图上的,还是需要滚动HTML内容?HTML不滚动,页面不够深。所以我设置了webView.scrollView.scrollEnabled=noc,你能提供一张你想做什么的图片吗?特别是,UIWebView中的漏洞是固定在后面的视图上的,还是需要滚动HTML内容?HTML不滚动,页面不够深。因此,我将webView.scrollView.scrollEnabled设置为否