1

I have the following UIPanGestureRecognizer set up:

- (void)pan:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
        CGPoint translation = [gesture translationInView:self.superview];
        gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);
        [gesture setTranslation:CGPointZero inView:self.superview];
    }
}

I want to pan around within the view, move one one place to another in the same view. What this is doing is moving the view around, how can I modify it to do what I want?

UPDATE: this is how my method ended up to pan around correctly

- (void)pan:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
        CGPoint translation = [gesture translationInView:self];;
        self.origin = CGPointMake(self.origin.x + translation.x, self.origin.y + translation.y);
        [gesture setTranslation:CGPointZero inView:self];
    } 
}

origin is a property in my view which just marks where the center of my graph is going to be. In its setter method there's a setNeedsDisplay to update whenever this value is changed

8vius
  • 5,786
  • 14
  • 74
  • 136
  • I don't understand what you're trying to do. Could you please clarify? – Johannes Fahrenkrug Feb 08 '12 at 01:16
  • I have a custom UIView, what the view does is graph an equation, say sin(x). I can scale my view by pinching, what I'm looking to do is to be able to move around the view, pan it, but not move the entire view around within it's super view, which is what's happening to me right now. – 8vius Feb 08 '12 at 01:30
  • I see. How about wrapping it in a UIScrollView? – Johannes Fahrenkrug Feb 08 '12 at 01:48
  • Thought about it, but I'm following the Stanford CS193P course on iOS development, and it hasn't been mentioned so far, and there has been no mention of it in the assignment tips. So I'm thinking there's another way. – 8vius Feb 08 '12 at 02:02
  • Quote: "what I'm looking to do is to be able to move around the view, pan it, but not move the entire view around within it's super view" So you just want to slide the graph horizontally but not freely move the whole view? Panning means moving the whole view though, like a photo in the Photos.app. That's not what you want? – Johannes Fahrenkrug Feb 08 '12 at 02:04
  • I want to move along my graph yes, not move the entire view around, which is what's happening. It's worth mentioning I don't own an iPhone, so I don't really know how the Photos app works. – 8vius Feb 08 '12 at 02:17
  • This is what happens when I pan: 1) http://yfrog.com/mxhvgp 2) http://yfrog.com/b7ns9p – 8vius Feb 08 '12 at 02:20
  • You probably really want to use a UIScrollView. But if not, just use the x value from your gesture and only adjust your view's x value and leave y alone. – Johannes Fahrenkrug Feb 08 '12 at 02:39
  • Probably, mind answering with how I'd implement the scrolling, so I can at least give you some points for the trouble? – 8vius Feb 08 '12 at 02:44
  • UIScrollView is serious overkill for this. Which view did you attach your pan gesture recognizer to? I expect that you added it to the view controller's view instead of your graph view. – Mark Adams Feb 08 '12 at 22:42
  • No, the gesture recognizer is added to the graph view. The view should handle it's own gestures, at least in this case. Anyways I already managed to pan around properly. I'll update my code so you can see how I ended up doing it. – 8vius Feb 09 '12 at 00:08

1 Answers1

6

OK, so (this is just off the top of my head, untested) if you want to just scroll horizontally without using a UIScrollView, try this:

- (void)pan:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
        CGPoint translation = [gesture translationInView:self.superview];
        gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y);
        [gesture setTranslation:CGPointZero inView:self.superview];
    }
}

All you do is leave out the + translation.y.

If you want to use a UIScrollView (which I'd recommend), see one of these tutorials: Are there any good UIScrollView Tutorials on the net?

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165