سؤال

I have a UIImageView that I am trying to move using a UIPanGestureRecognizer object. The UIImageView is positioned over top of a UITableView, and serves as a scrollbar. I want this UIImageView to be moved by the user up or down the UITableView, not sideways. To accomplish this, I have implemented UIGestureRecognizerDelegate, and I have the following methods:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

     return YES;

}

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {

    NSLog(@"Do I get called?");
    //_startLocation is a property of type CGPoint that I declare in the .h file
    _startLocation = [recognizer locationInView:_imageView];

    NSLog(@"The point is: %d", _startLocation);

    CGRect frame = [_imageView frame];

    frame.origin.y += frame.origin.y - _startLocation.y;

    [_imageView setFrame: frame];

    _imageView.center = _startLocation;

} 

The method, panGestureDetected is called from viewDidLoad as follows:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
    panGesture.maximumNumberOfTouches = 1;
    panGesture.minimumNumberOfTouches = 1;
    panGesture.delegate = self;
    [_table addGestureRecognizer:panGesture];

Unfortunately, my UIImageView moves all over the place frantically on the screen when I try to move it. I want to see a smooth scrolling UIImageView go up/down while the user drags it. Can anyone see what I am doing wrong?

هل كانت مفيدة؟

المحلول

In your handler method just keep this line and remove all other.

_imageView.center.y = [recognizer locationInView:[_imageView superView]].y; 

You need to get location in superView, not imageView itself. And just change the y value of center.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top