views:

47

answers:

2

Hi all,

How can I detect if a user holds their finger down on an onscreen object for 1 second ?

Thanks,

Martin

+1  A: 

Have a look at the timestamp property of UITouch Class

timestamp The time when the touch occurred or when it was last mutated. (read-only)

@property(nonatomic, readonly) NSTimeInterval timestamp Discussion The value of this property is the time, in seconds, since system startup the touch either originated or was last changed. You can store and compare the initial value of this attribute to subsequent timestamp values of the UITouch instance to determine the duration of the touch and, if it is being swiped, the speed of movement. For a definition of the time-since-boot value, see the description of the systemUptime method of the NSProcessInfo class.

You should use it with

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

in UIResponder or UIGestureRecognizer classes according to your target OS

notme
+1  A: 

You need at least one state variable and a timer.

On a touch down inside inside the target area, set the state variable with the current time and start a 1 second timer. On any touch move outside the target area or touch up event, clear the state variable. After the 1 second timer has gone off, check the state variable. If it still has a time from 1 second ago or more (and thus hasn't been cleared or changed), then the touch has been on target for at least 1 second.

hotpaw2
Thanks Hotpaw - would you be able to post some sample code or point me to some on the web ?
Ohnomycoco