Reputation: 17
everyone! I need to detect 2 touches on different image views in same time. So I need to start timer when user touch both image views in same time. And stop timer when touches is end. The image views are moving on screen. There are no problems when I use one image view. Have you any ideas?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if ([touchArea2.layer.presentationLayer hitTest:location2]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
}
if ([touchArea.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];
timerTouch = 10;
touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
} else if (![touchArea.layer.presentationLayer hitTest:location]){
[touchTimer invalidate];
touchTimer = nil;
} }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches) {
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if (![touchArea.layer.presentationLayer hitTest:location]){
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
touchArea.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
touchTimer = nil;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
Thanks for any help))
Upvotes: 0
Views: 316
Reputation: 225
UIView * view = [[UIView alloc] init];
UITapGestureRecognizer * tap = nil;
tap.numberOfTouchesRequired = 2;
tap.delegate = self;
[view addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
then you recognizer the location of two touches,and decide what you do ;
Upvotes: 0
Reputation: 17
I thought about this problem a lot and I find the way how to do that. Here is my code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
for (UITouch *touch2 in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
CGPoint location2 = [touch2 locationInView:touch2.view];
if ([touchArea2.layer.presentationLayer hitTest:location]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
}
if ([touchArea.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];
if ([touchArea2.layer.presentationLayer hitTest:location2]) {
touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
timerTouch = 10;
touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
} }}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
if (![touchArea.layer.presentationLayer hitTest:location]&&![touchArea2.layer.presentationLayer hitTest:location]) {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchArea.image = [UIImage imageNamed:@"TouchArea"];
touchArea2.image = [UIImage imageNamed:@"TouchArea"];
[touchTimer invalidate];
}
Upvotes: 0
Reputation: 2083
You may want to have a look at UIGestureRecognizerDelegate
and then the method:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
Upvotes: 1
Reputation: 6524
Try this :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == firstimage ) { // Do Something}
if ([touch view] == secondimage ) { // Do Something}
}
Upvotes: 0