Note that there are some explanatory texts on larger screens.

plurals
  1. POUIWebView addSubview:UIImageView not displaying when triggered by touch events
    text
    copied!<p>A new day, a new question!</p> <p>I have been working on an app that requires I show a circle where the users finger(s) is(are) when they are touching the screen.</p> <p>I have followed a <a href="http://wyldco.com/blog/2010/11/how-to-capture-touches-over-a-uiwebview/" rel="nofollow">tutorial</a> I found on subclassing UIWindow, and <a href="http://wyldco.com/blog/2010/11/how-to-capture-touches-over-a-uiwebview/#comment-225" rel="nofollow">my comment</a> explains how to pass on those touches if you are using Storyboards and ARC.</p> <p>I am using the following code (which is based on <a href="http://mobile.tutsplus.com/tutorials/iphone/ios-quick-tip-detecting-touches/" rel="nofollow">this tutorial</a>) to make a touch indicator to display:</p> <pre><code>#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"); } </code></pre> <p>UserTouchImageView is a subclass of UIImageView, with the change to the default being:</p> <pre><code>- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.image = [UIImage imageNamed:@"greyCircle.png"]; self.alpha = 0.5f; } return self; } </code></pre> <p>I've tested this code without a UIWebview (replacing the <code>self.webView</code> with <code>self.view</code>), and it works fine, drawing the circle where I click and drag, then fading out when I release the button.</p> <p>I have also succeeded in instantiating the UserTouchImageView and adding it to the webView from the <code>viewDidLoad</code> method of the viewController.</p> <p>As soon as I put the UIWebview in with the code above, the same line (<code>[self.webView addSubview:touchView];</code>) doesn't work. I still get the NSLog messages to the console, but the subview doesn't get visibly added.</p> <p>Can anyone help me figure out why this doesn't appear? Is there any other code I need to be aware of, or any reason why I cannot do this?</p> <p>Many thanks!</p> <p><strong>EDIT</strong></p> <p>I have uploaded my source so far <a href="http://www.mediafire.com/?y3sfy920sfama9a" rel="nofollow">here (MediaFire)</a></p> <p><strong>EDIT 2</strong></p> <p>I've added two methods as below:</p> <pre><code>-(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]; } </code></pre> <p>These are called from two buttons outside the UIWebView, and from within the touchesbegan method:</p> <pre><code>- (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"); } </code></pre> <p>The logging seems to indicate that the webview referenced by <code>self.webView</code> <strong>WITHIN</strong> the touches began method is an entirely seperate instance than the one when referenced from the two buttons. I can add multiple subviews via the button, and then list them, and they all show up in the logging, yet when I click to touch on the simulator, none of these extra subviews get listed.</p> <p>Does anyone know of any special functionality in a UIWebView to have a duplicate instance on touch events?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload