how to count tap in simulator? - objective-c

how do i count double tap in simulator?

- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 2)
{
// do your stuff here
}
}

Implement one of the touch functions defined in UIResponder (touchesBegan, touchedEnded, etc...). When you get the touches array, you can get a UITouch's tap count using code like this:
UITouch * t = [touches anyObject];
NSLog(#"%d", [t tapCount]);

Related

how to do imageView touch event under the scrollview?

I am using - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event touch method in imageView but I have taken imageView under the scrollview thats why touch event not perform because it is take directly scrollview
My code is
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imagedisplay) {
// move the image view
imagedisplay.center = touchLocation;
}
}
imagedisplay my imageView object but in not work touch event I have work in scrollview in that taken imageView so give any solution and source code related touch which apply in both scrollview and imageView
use below code, where you are creating imageView-
imageView.userInteractionEnabled = YES;
As it is NO by default.

-(void)touchesBegan (TO A SPECIFIED UIIMAGEVIEW)

Hi I want to make it so that if you touch an image that I put on the view the -(void)checkcollision happens. The -(void)checkcollision happens, but when I touch anything. How can I say it only works if i touch a specified image. e.g. a pimple:
IBOutlet UIImageView *pimple;
#property (nonatomic, retain) UIImageView *pimple;
here is the touchesBegan code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
[self checkcollision];
}
-(void)checkcollision {
if (label.text = #"0") {
label.text = #"1";
}
pimple.hidden = YES;
}
CGPoint point = [myTouch locationInView:pimple];
if ( CGRectContainsPoint(pimple.bounds, point) ) {
... Touch detected.
}
Alternatively you can consider gesture recognizers. You can use a tap recognizer for this case.
There are two options:
Subclass UIImageView and define your own handler
Check sender - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event and compare it to your UIImageView instance.

How to get the location of a touch in touchesBegan function

I tried the following code, but it doesn't work. How should it be modified?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
location = [touches locationInView:self];
}
In the h files I have defined location like this:
CGPoint location;
The (NSSet *)touches will give you all the current touches on the screen. You will need to go on each touch on this set and get it's coordinate.
These touches are members of UITouch class. Have a look at the UITouch class reference.
for instance you would do:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView:self];
// Do something with this location
// If you want to check if it was on a specific area of the screen for example
if (CGRectContainsPoint(myDefinedRect, location)) {
// The touch is inside the rect, do something with it
// ...
// If one touch inside the rect is enough, break the loop
break;
}
}
}
Cheers!

Can't we use touchesBegan,touchesMoved,touchesEnded for a scrollview?

Actually I'm having a scroll-view. In that I'm using 30-buttons. what's my requirement is I need to rearrange the buttons. Like, when i touched any button it should be selected with our touch. and where ever we move in the scroll-view it should move along with our touch. after i ended the touch, the buttons should be swapped. Can any one help me regarding this.........
You can do this behavior but there a lot of work. You need next:
1. Create UIControl subclass that will be your buttons.
2. Override all touches* methods there.
3. Implement there support of standard uicontrol behavior + moving behavior.
#pragma mark -
#pragma mark Touches delegate methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self performSelector:#selector(delayedTouchBeging:) withObject:touch afterDelay:0.15];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ( dragging ) {
// move your view in new position here
} else {
[super touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ( dragging ) {
// Do other stuff if you need
} else {
[super touchesEnded:touches withEvent:event];
}
dragging = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
- (void) delayedTouchBeging:(UITouch*) touch {
dragging = YES;
[self cancelTrackingWithEvent:nil];
// Do here stuff about begin moving (animation, etc)
}
You may want to look into how they do the "Launcher" code as a part of the three20 code catalog that's available for free. They do exactly something like this (and mimicks the iPhone apps view where you can move apps around or delete them).
http://github.com/facebook/three20

Using an Instance Variable in more than one method

How would I save an Instance so that I could use it in another method?
This is the code I have:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchedStart;
// find position of the touch
touchedStart = [[touches anyObject] locationInView:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchedEnd;
// find position of the touch
touchedEnd = [[touches anyObject] locationInView:self];
}
I want to be able to use the touchedStart variable in the touchesEnded method. How would I do this?
Just make it a member variable in the class itself - then the other method will be able to access it.