iOS WKWebView получает цвет пикселя RGBA из точки

Как я могу получить цвет точки RGBA от WKWebView?

У меня есть рабочее решение для UIWebView, но я бы хотел использовать вместо него WKWebView. Когда я нажимаю на точку экрана, я могу получить значение цвета в RGBA из UIWebView, например (0,0,0,0), когда оно прозрачное или что-то вроде (0,76,0.23,0.34,1), когда это не прозрачно. Вместо этого WKWebView всегда возвращает (0,0,0,0).

Подробнее

Я работаю над приложением iOS с WebView в качестве самого популярного элемента пользовательского интерфейса.

The WebView has areas which are transparent so that you can see the the underlying UIView.

The WebView shall ignore touches on transparent areas and the underlying UIView shall retrieve the event instead.

Therefor I did override the hitTest function:

#import "OverlayView.h"
#import <QuartzCore/QuartzCore.h>

@implementation OverlayView

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

    UIView* subview = [super hitTest:point withEvent:event];  // this will always be a webview

    if ([self isTransparent:point fromView:subview.layer]) // if point is transparent then let superview deal with it
    {
        return [self superview];
    }

    return subview; // return webview
}

- (BOOL) isTransparent:(CGPoint)point fromView:(CALayer*)layer
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return (pixel[0]/255.0 == 0) &&(pixel[1]/255.0 == 0) &&(pixel[2]/255.0 == 0) &&(pixel[3]/255.0 == 0) ;
}

@end

My assumption is that the WKWebView has a different CALayer or an hidden UIView where it draw the actual webpage to.

1 ответ

Решение
#import "OverlayView.h"
#import <QuartzCore/QuartzCore.h>

@implementation OverlayView

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

    UIView* subview = [super hitTest:point withEvent:event];  // this should always be a webview

    if ([self isTransparent:[self convertPoint:point toView:subview] fromView:subview.layer]) // if point is transparent then let superview deal with it
    {
        return [self superview];
    }

    return subview; // return webview
}

- (BOOL) isTransparent:(CGPoint)point fromView:(CALayer*)layer
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y );

    UIGraphicsPushContext(context);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIGraphicsPopContext();

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return (pixel[0]/255.0 == 0) &&(pixel[1]/255.0 == 0) &&(pixel[2]/255.0 == 0) &&(pixel[3]/255.0 == 0) ;
}

@end

Этот код исправил мою проблему.

Другие вопросы по тегам