Is there a way to cancel touch events in certain regions of a view? I have a custom UIView and I only want to process touch events only if they are say 100 pixels from the edges of the screen.
As Justin said, add a custom UIView in interface builder (or programmatically) and add it to the view. let's call that view touchArea. Then in your Viewcontroller.m file, implement the
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
methods (depends on what you're trying to do), and in these do:
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
if (CGRectContainsPoint(touchArea.frame, location)) {
//code to execute
}
actually, I think even a CGRect as an instance variable that is placed on the view can work, but the above is how I achieved it.
Assuming you're using Interface Builder, simply take another UIView. Resize it, and place it where you want touch events to occur. Then, have the custom view able to be touch active. Programmatically, I, personally, do not know how to do it.
Related
I want to create custom annotation animations like in this app:- Easy Taxi iOS. In this app when the user is dragging the map, annotation is lifted a little. Then when dragging is stopped, it falls down from the lifted position with a nice animation.
So far i came to know that, I can use a static image in the center of my screen to show the movement on the map, & when i stop dragging the view(map) i have to build a custom animation for the annotation view.
So far i have implemented this code by which i am manipulating an Image to be hidden or shown on the map View in the center of the screen.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
for (UITouch * touch in touches) {
CGPoint loc = [touch locationInView:self.mapView];
if ([self.mapView pointInside:loc withEvent:event]) {
self.mapKitAnnotationimageView.hidden=NO;
imageCheck=NO;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
imageCheck=NO;
self.mapKitAnnotationimageView.hidden=YES;
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if (!imageCheck) {
self.mapKitAnnotationimageView.image=[UIImage imageNamed:#"location"];
imageCheck=YES;
}
}
Any help regarding the custom animation will be greatly appreciated. I need this done in very short span of time.
Thanks for your time.
I use the following code to move an UIImageView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
paddle.center = CGPointMake(location.x, paddle.center.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}
It works great, moving from left to right. But i can't figure out how to avoid moving over the screen, i meant to move it but don't intersect right and left edges of my screen. let's say 10 pixels from left and 10 from right.
First of all, you are the one saying this:
paddle.center = CGPointMake(location.x, paddle.center.y);
So where paddle is, is entirely up to you. If you don't want it at a certain x value, don't set it to that x value.
Second, never never call touchesBegan from within touchesMoved.
Third, you'd be much happier and safer using a UIPanGestureRecognizer. Situations like this are exactly what it's for.
Can't seem to find an answer to this one anywhere.
My game starts when the user touches the screen, there is a path that the finger must stay within, if it touches/intersects the edges then I want it to run the method [self gameover].
The edge will be a UIImageView.
Thanks everyone.
I hope you have overridden the touches moved event. check either the user touch intersects your image view like below.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point =[touch locationInView:self.view];
if (CGRectContainsPoint(self.imageView.frame, point))
{
//Intersects
[self gameOver];
}
}
I want to handle in superview a touch event when there are two touches that land on different subviews.
Can I do it somehow by adding UITapGestureRecognizer with numberOfTouchesRequired =2; with a target self to a subviews?
Or I need to go something more complicated?
You could try something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint referencePoint = [touch locationInView:self.view];
if([touch tapCount] == 2){
//Test touch coordinates to see if theres one in each view
}
I am trying to get the coordinates from a touch but I can't figure out how.
I am in a view controller and I try to use the method -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event but I think it doesn't work with view controllers.
Am I right?
I just want to put a Button on the position I touched.
Thanks in advance!
It belongs in a View rather than ViewController, and would look something like this:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)evt {
UITouch *touch=[touches anyObject];
CGPoint pt=[touch locationInView:self];
// ...make your button at 'pt'...
}