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
}
}
Related
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));
}
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.
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
I was trying to create a label that should be draggable. But the dragged event is not firing (checked with break point). Below is the code.
- (IBAction)InsertText:(UIBarButtonItem *)sender {
UILabel *lblWatermark = [[UILabel alloc] initWithFrame:currentImage.frame];
lblWatermark.text = #"Copyright";
lblWatermark.userInteractionEnabled = YES;
[lblWatermark sizeToFit];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[lblWatermark addGestureRecognizer:gesture];
[currentImage addSubview:lblWatermark];
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:label];
}
Thank you in advance,
Spotted the error. Actually I had also to make
userInteractionEnabled = YES
Not only for the label But also for the Imageview in which I am adding the label.
Make sure your ViewController is the delegate for UIGestureRecognizer with (.h file):
#interface View : UIView <UIGestureRecognizerDelegate>
And then set:
gesture.delegate = self;
I'm showing a UIPopoverController in a ViewController, and I want to drag one image from the popover to the ViewController.
I know that to do it, I need to use a UIGestureRecognizer, but I don't know how. I can move the image around the popover, but I can't drag and drop it to the ViewController.
My code:
-(void)viewDidLoad
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
}
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
CGPoint newCenter = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = ball.center.x;
beginY = ball.center.y;
}
newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);
[ball setCenter:newCenter];
}