iOS: Touch Event in UIScroll view - ios

I have tried so many thing to get touch inside a UIScrollView but I'm not getting how to do it.
Actually I want to touch inside a UIScrollView and get the location of subview (which i have touched).

You can do it with gesture recognizers. For detect single tap location use UITapGestureRecognizer
UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[myScrollView addGestureRecognizer:tapRecognizer];
- (void)tapAction:(UITapGestureRecognizer*)sender{
CGPoint tapPoint = [sender locationInView:myScrollView];
CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view];
}
To convert that tapPoint to self.view you can use convertPoint:toView: method in UIView class

Related

How to get CGPoint where a gesture is happening in Objective C

I have added a gesture recognizer because I want to get the x coordinate of a touch (working on an audio trimmer, so I need the x coordinate for the handles of the trimmer).
I have
UIGestureRecognizer *recognizer;
CGPoint leftControlPoint = [recognizer locationInView:self.view];
NSLog(#"this is where the touch is %#", NSStringFromCGPoint(leftControlPoint));
However, the NSLog simply gives me {0,0}.
Any idea what's going on?
Thanks what I can do?
The proper way to wire up a gesture recognizer is something like this:
// In some method, probably -viewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(didTapView:)];
[self.view addGestureRecognizer:tap];
Then:
- (void)didTapView:(UITapGestureRecognizer *)sender {
CGPoint leftControlPoint = [sender locationInView:self.view];
NSLog(#"this is where the touch is %#", NSStringFromCGPoint(leftControlPoint));
}

Using UIPanGestureRecognizer to scroll smoothly like UITableView or UIScrollView

I am using UIPanGestureRecognizer to slide/scroll a UIView. The following code works well and I am able to slide/scroll the panView. My question is when I end the gesture I should make the panView(UIView) scroll smoothly smiler like UITableView or UIScrollView scroll. So please help me out what should I code in UIGestureRecognizerStateEnded state. Please refer the below code. Thanks in advance!!
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scrollpanView:)];
[panRecognizer setDelegate:self];
[panView addGestureRecognizer:panRecognizer];
-(void)scrollpanView:(id)sender{
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint translatedPoint = [gesture locationInView:[panView superview]].x;
if (UIGestureRecognizerStateBegan){
panXAxis = [gesture locationInView:[panView superview]].x;
translatedPoint = CGPointMake(panXAxis+translatedPoint.x, 50);
[panView setCenter:translatedPoint];
}
if ( UIGestureRecognizerStateChanged){
translatedPoint = CGPointMake(panXAxis+translatedPoint.x, 50);
[panView setCenter:translatedPoint];
}
if (UIGestureRecognizerStateEnded){
CGPoint velocity = [gesture velocityInView:[panView superview]]
// How should I scroll smoothly using the 'velocity' value
}
}

Zoom with touches in a custom view in a scrollView. touchesMoved not called

I have a custom view that will pinch zoom, but I'd like to also slide it around in any direction. The zoom works well, and the moving works well, but so far it's just one or the other. The touchesMoved method never gets called.
The scrollView has a subView which the user will touch and move. It will function like a little map. On the subView are several custom labels. I have a tap Gesture recognizer on the labels, which works fine now, and will still need to work when the zoom and moving is corrected.
I also tried [scrollView setScrollEnabled:NO]; and other things, but that didn't allow both to work. How can touches Moved be called? What will I need to do to allow both to work?
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setMinimumZoomScale:.7];
[scrollView setMaximumZoomScale:1.5];
[scrollView setAutoresizesSubviews:YES];
[scrollView setUserInteractionEnabled:YES];
scrollView.delegate = self;
[self.view addSubview:scrollView];
CGRect mapFrame = CGRectMake(0, 0, 300, 300);//temp numbers
subView = [[UIView alloc]initWithFrame:mapFrame];
subView.userInteractionEnabled=YES;
subView.autoresizesSubviews=YES;
[scrollView addSubview:subView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesMoved");
touch = [touches anyObject];
CGPoint location = [touch locationInView:subView];
CGPoint prevLoc = [touch previousLocationInView:subView];
CGPoint origin = subView.frame.origin;
origin.x += (location.x - prevLoc.x);
origin.y += (location.y - prevLoc.y);
CGRect newFrame = subView.frame;
newFrame.origin = origin;
subView.frame = newFrame;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return subView;
}
//label sample
newCity =[[MyCustomClass alloc] initWithFrame:CGRectMake(x, y, 95, 40)];
newCity.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(touchUp:)];
[newCity addGestureRecognizer:tapGesture];
[subView addSubview:newCity];
I'm also getting this in the log and don't know why. "Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since gesture recognizer is not active.
" I removed the pan gesture recognizer, after that didn't work. I'm using Xcode 4.5.2
My header is
#interface CitiesViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
{
UIView *subView;
}
#property(nonatomic, strong) UIView *subView;
#property(nonatomic, strong) UIScrollView *scrollView;
To solve this I removed the TouchesMoved entirely and used a panGestureRecognizer instead. Hopefully it helps someone.
I added this to my subView:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
[pan setDelegate:self];
[subView addGestureRecognizer:pan];
Add this method:
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:subView];
subView.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
}
I never did figure out why that message Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] is still occurring, but this seems to work.

Why tap gesture recognizer recognizes the taps from the view on its parent view?

I have a UIView and a tap gesture recognizer in it:
UIImageView *tabView = [[UIImageView alloc] initWithFrame:CGRectMake(41, 145, 702, 100)];
tabView.image = [UIImage imageNamed:#"inactive_tab"];
tabView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[tabView addGestureRecognizer:singleFingerTap];
[self.scrollView addSubview:tabView];
And I add another view on scrollview:
[self.scrollView addSubview:self.activeTab];
activeTab is over the inactiveTap. When I tap to activeTap, gesture recognizer fires, whis I dont want to be happened. How can I avoid this?
Use UIGestureRecognizerDelegate and its method gestureRecognizer:shouldReceiveTouch:.
You can check if the touch point is inside a view frame and return NO if you don't want the touch to happen on that view.`
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint touchLocation = [touch locationInView:self.view];
return !CGRectContainsPoint(self.activeTab.frame, touchLocation);
}

Forward pan/pinch gesture to mkmapview

I have a UIViewController with a MKMapView and an other View (lets say PaintView) on top of the mapView.
When a user touches the PaintView with a pinch gesture, the mapview underneath should respond (as well as the paintView self).
When a user touches the Paintview with a pan gesture the mapview must not respond. (the pan gesture is used for painting).
Is there any possibility to forward the pinch gesture to the mapView?
[self.view addSubview:self.mapView];
[self.view addSubview:self.paintView];
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[self.paintView addGestureRecognizer:panRecognizer];
panRecognizer.delegate=self;
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[self.paintView addGestureRecognizer:pinchGesture];
pinchGesture.delegate=self;
-(void)pan:(UIPanGestureRecognizer *)gesture
{
//do sth in the paintView
}
-(void)pinch:(UIPinchGestureRecognizer *)gesture
{
//forward pinch gesture to mapview
//do sth in paintview
}

Resources