Custom UIView not receiving touch events for fast mouse clicks in simulator

I'm having a problem in the simulator where if I click fast in a UIView inside a table cell that is setup to receive touch events, it doesn't receive them. But if I click slow (holding down the button for a little bit) the view gets the touch events. I can't test it on the device. Is this a known problem in the simulator?

Thanks.

MacBook Pro, Mac OS X (10.5.2), iPhone OS

Posted on Jun 3, 2008 2:21 PM

Reply
9 replies

Jun 17, 2008 9:47 AM in response to applecoder

I am having a related issue:
In my applicaton I want to subclass UITableViewController to create a tableview and there is a picture in one Cell, I am trying catch touch events in that cell, like move right to display next picture and move left to display previous picture.
I subclassed UITableViewCell( subclass of UIView) to create CustomCell, but I could not get the touch event. Did the CustomCell's superview get the events and not dispatch to the CustomCell?
How can you implement this? Any suggestion are highly appreciated, Thanks a lot.

Jun 17, 2008 3:41 PM in response to Victor Zhang

In previous SDK versions I think I remember there being this "delay" behavior -- but I don't see it now in a quick test app that I threw together. Maybe this has something to do with the delaysContentTouches of UIScrollView (and thus UITableView, which inherits from it)?

As for handling events on a UIImageView, there are some other threads on this in the forum. But from my experiementing I have come to the conclusion that UIScrollView "handles" all touch events. That means that any view below a UIScrollView (or below inherited classes like UITableView) will not propogate "unhandled" events back up above the UIScrollView. This is why a UIViewController will not receive the touches*: method calls if a touch occurs in a UIScrollView. If no subview of the UIScrollView handles those touches, the propogate back up to the UIScrollView which then stops their propogation.

The only way I have found to handle events in views below a UIScrollView is with custom subclasses. That being said, your custom UITableViewCell should be calling touchesBegan/touchesEnded/etc, assuming you implemented them. I just wrote a quick test app and it's working fine. Of course, implementing at this level means you need to test whether the touch was within the bounds of the Image subview you have. Another way would be to subclass UIImageView and implement the touches*: methods.

Also be sure userInteractionEnabled is "YES" for the custom UIImageView, if you go this route, so that it reports it can respond to events.

Hope this helps,
George

Message was edited by: gkstuart

Jun 17, 2008 7:14 PM in response to gkstuart

Hi George,

Thanks a lot for your reply. I just use CustomCell.image to hold my pictures, I implemented touchesBegan/touchesEnded to catch any touch events, and I am sure I initialized the custom cell with userInteractionEnabled = YES. But I still could not get the touch events, is there anything I still missed?

Will try your suggestion for custom UIImageView approach later, Thanks again.
Victor

Jun 17, 2008 8:53 PM in response to Victor Zhang

Victor -- I had thought you were adding a subview, my mistake.

Given the new scenario, using the cell.image property means you don't use a UIImageView (you use a UIImage instead). UIImage is not a descendant of UIResponder so it doesn't handle events.

However, the custom cell should still get the touchesBegan: message. I just tried it and the event is reliably delivered to my custom UITableViewCell. Of course, this event is delivered for touches anywhere in the cell. Perhaps that what you are looking for? I guess you could determine if the touch is in the bounds of the placed image... but I think it would be easier to place a subview that simply handles the events.

Cheers,
George

Jun 18, 2008 12:35 AM in response to gkstuart

Hi George,

Thanks a lot for your quick response and jumping to help.
I am so frustrated that I did not get touch event for the custom cell.
I also tried your solution for a custom UIImageView, I put the codes of PhotoView.h, PhotoView.m and TestTouchAppDelegate.m here: Please help to look into it for me.

//
// PhotoView.h
// Molinker
//
// Created by Victor on 6/18/08.
// Copyright 2008 _MyCompanyName_. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface PhotoView : UIImageView {

CGPoint touchPoint1;
CGPoint touchPoint2;

}
@property (nonatomic) CGPoint touchPoint1;
@property (nonatomic) CGPoint touchPoint2;

@end




//
// PhotoView.m
// Molinker
//
// Created by Victor on 6/18/08.
// Copyright 2008 _MyCompanyName_. All rights reserved.
//

#import "PhotoView.h"


@implementation PhotoView
@synthesize touchPoint1;
@synthesize touchPoint2;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
self.userInteractionEnabled = YES;
}
return self;
}


- (void)drawRect:(CGRect)rect {
// Drawing code
}


// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];
touchPoint1 = [touch locationInView:self];
}

// Handles the end of a touch event.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
touchPoint2 = [touch locationInView:self];

if(touchPoint1.x>touchPoint2.x)
{
//move Left
NSLog(@"Move Left");

}
if(touchPoint1.x<touchPoint2.x)
{
//move Right
NSLog(@"Move Right");
}
}

- (void)dealloc {
[super dealloc];
}

@end



//
// TestTouchAppDelegate.m
// TestTouch
//
// Created by Victor on 6/17/08.
// Copyright _MyCompanyName_ 2008. All rights reserved.
//

#import "TestTouchAppDelegate.h"
#import "RootViewController.h"
#import "PhotoView.h"


@implementation TestTouchAppDelegate

@synthesize window;
@synthesize navigationController;


- (id)init {
if (self = [super init]) {
//
}
return self;
}


- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configure and show the window
//[window addSubview:[navigationController view]];

UIImage *myImage= [UIImage imageNamed: @"scene1.jpg"];

CGRect frame = CGRectMake (0, 20, 300, 400);
PhotoView *myImageView1 = [[UIImageView alloc ] initWithFrame:frame];
myImageView1.userInteractionEnabled = YES;
myImageView1.image = myImage;
[window addSubview:myImageView1];

[window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}


- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}

@end

Message was edited by: Victor Zhang

Jul 21, 2008 7:56 AM in response to applecoder

I'm having a very similar problem. Only very lengthy touches are registered by the UIButtons sitting on a UIView that's on a scrollview. It appears that the scrollview is intercepting the touch events to check for scrolling, but the threshold for scrolling is set to a level such that by buttons often don't look pushed unless the touch is held for a moment.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Custom UIView not receiving touch events for fast mouse clicks in simulator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.