objective-c - UIWebView addSubview :UIImageView not displaying when triggered by touch events

标签 objective-c ios uiwebview subview

新的一天,新的问题!

我一直在开发一个应用程序,该应用程序需要在用户触摸屏幕时显示用户手指所在的圆圈。

我关注了 tutorial我发现子类化 UIWindow 和 my comment如果您使用 Storyboards 和 ARC,说明如何传递这些内容。

我正在使用以下代码(基于 this tutorial )来显示触摸指示器:

#pragma mark - Touch Events
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    NSArray *subviews = [self.webView subviews];
    for (UIView *view in subviews) 
    {
        // Check if view is scrollview
        if ([view isKindOfClass:[UserTouchImageView class]])
        {
            [view removeFromSuperview];
        }
    }

    // Enumerate over all the touches and draw a red dot on the screen where the touches were
    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        // Get a single touch and it's location
        UITouch *touch = obj;
        CGPoint touchPoint = [touch locationInView:self.webView]; 

        // Add touch indicator
        UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
        [self.webView addSubview:touchView];
    }];
    NSLog(@"Touches began");
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    NSArray *subviews = [self.webView subviews];
    for (UIView *view in subviews) 
    {
        // Check if view is scrollview
        if ([view isKindOfClass:[UserTouchImageView class]])
        {
            [view removeFromSuperview];
        }
    }

    // Enumerate over all the touches and draw a red dot on the screen where the touches were
    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        // Get a single touch and it's location
        UITouch *touch = obj;
        CGPoint touchPoint = [touch locationInView:self.webView]; 

        // Draw a red circle where the touch occurred
        UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
        [self.webView addSubview:touchView];
    }];
    NSLog(@"Touches moved");
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    NSArray *subviews = [self.webView subviews];
    for (UIView *view in subviews) 
    {
        // Check if view is scrollview
        if ([view isKindOfClass:[UserTouchImageView class]])
        {
            [UIView animateWithDuration:0.5f 
                             animations:^{
                                 view.alpha = 0.0;
                             }
                             completion:^(BOOL finished) {
                                 [view removeFromSuperview];
                             }];
        }
    }
    NSLog(@"Touches ended");
}
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
    NSArray *subviews = [self.webView subviews];
    for (UIView *view in subviews) 
    {
        // Check if view is scrollview
        if ([view isKindOfClass:[UserTouchImageView class]])
        {
            [view removeFromSuperview];
        }
    }
    NSLog(@"Touches cancelled");
}

UserTouchImageView 是 UIImageView 的子类,默认更改为:
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.image = [UIImage imageNamed:@"greyCircle.png"];
        self.alpha = 0.5f;
    }
    return self;
}

我已经在没有 UIWebview 的情况下测试了此代码(将 self.webView 替换为 self.view ),它工作正常,在我单击并拖动的地方绘制圆圈,然后在我释放按钮时淡出。

我还成功实例化了 UserTouchImageView 并将其从 viewDidLoad 添加到 webView View Controller 的方法。

一旦我将 UIWebview 与上面的代码一起放入,同一行 ([self.webView addSubview:touchView];) 就不起作用。我仍然将 NSLog 消息发送到控制台,但 subview 没有明显添加。

谁能帮我弄清楚为什么这没有出现?是否有任何其他代码我需要注意,或者我不能这样做的任何原因?

非常感谢!

编辑

到目前为止,我已经上传了我的源 here (MediaFire)

编辑 2

我添加了以下两种方法:
-(void)listWebViewSubViews
{
    NSLog(@"BEGIN LISTING SUBVIEWS");
    for (UIView *view in [self.webView subviews]) {
        NSLog(@"Subview: %@", view.description);
    }
    NSLog(@"END LISTING SUBVIEWS");
}
-(void)view:(UIView *)view addTouchIndicatorWithCentreAtPoint:(CGPoint)point
{
    NSLog(@"View: %@, x: %f, y: %f", view.description, point.x, point.y);

    // Add touch indicator
    UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(point.x -15, point.y -15, 30, 30)];

    [view addSubview:touchView];


}

这些是从 UIWebView 外部的两个按钮和 touchesbegan 方法中调用的:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    NSArray *subviews = [self.webView subviews];
    for (UIView *view in subviews) 
    {
        if ([view isKindOfClass:[UserTouchImageView class]])
        {
            [view removeFromSuperview];
        }
    }

    [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        UITouch *touch = obj;
        CGPoint touchPoint = [touch locationInView:self.webView]; 

        [self view:self.webView addTouchIndicatorWithCentreAtPoint:touchPoint];

        [self listWebViewSubViews];
    }];
    NSLog(@"Touches began");
}

日志似乎表明 self.webView 引用的 web View 内touches begin 方法是一个完全独立的实例,而不是从两个按钮引用的实例。我可以通过按钮添加多个 subview ,然后列出它们,它们都显示在日志中,但是当我点击模拟器时,这些额外的 subview 都没有列出。

有谁知道 UIWebView 中有任何特殊功能在触摸事件上有重复的实例?

最佳答案

一个 UIWebView不是一个 View ,而是 View 的集合。当您将 subview 添加到 UIWebView 时,新的 subview 将添加到 subview 的层次结构中。您的新 subview 可能被添加到其他不透明 View 后面。它在那里,但隐藏起来。我建议将此指示器 View 添加到层​​次结构中的不同 View 。

关于objective-c - UIWebView addSubview :UIImageView not displaying when triggered by touch events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11565600/

相关文章:

java - 如何在appium中接受这个警报?

iOS 8 键盘扩展 - UIInputViewController 无法添加多个 View

objective-c - IOS 上的波形

ios - 在 iOS 中让 UIImageView 的一部分无形化

ios - 在没有 NSNotificationCenter 的情况下,在 iOS 8 中正确强制或允许 Youtube 嵌入式视频的横向模式

objective-c - 无法使用简单的立方体显示 VBO,似乎是缓冲区的问题

ios - PFQuery 的奇怪行为

iphone - 使用 UIGradientLayer 作为 layer.mask 淡出 ScrollView 的底部/顶部 --> 掩码的重新定位滞后

ios - 尝试从对象中插入 nil 对象

ios - UIActivityViewController 在使用 sourceView 或 barButtonItem 的 iPad 上崩溃