Tas*_*aff 7 pixel objective-c rgba ios wkwebview
如何从WKWebView获取点的RGBA像素颜色?
我有一个UIWebView的工作解决方案,但我想使用WKWebView.当我点击屏幕上的某个点时,我能够从UIWebView中检索RGBA中的颜色值,例如(0,0,0,0),当它是透明的时候(0.76,0.23,0.34,1)它不透明.WKWebView总是返回(0,0,0,0).
更多细节
我正在开发一个iOS应用程序,其中WebView是最顶级的ui元素.
WebView具有透明的区域,因此您可以看到底层的UIView.
WebView将忽略透明区域上的触摸,而底层UIView将检索事件.
因此我确实覆盖了hitTest函数:
#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
Run Code Online (Sandbox Code Playgroud)
我的假设是WKWebView有一个不同的CALayer或隐藏的UIView,它绘制实际的网页.
#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
Run Code Online (Sandbox Code Playgroud)
这段代码解决了我的问题.
| 归档时间: |
|
| 查看次数: |
703 次 |
| 最近记录: |