0

我有一个 UIImageView,我正在尝试使用 UIPanGestureRecognizer 对象移动它。UIImageView 位于 UITableView 之上,用作滚动条。我希望这个 UIImageView 由用户向上或向下移动 UITableView,而不是横向移动。为此,我实现了 UIGestureRecognizerDelegate,我有以下方法:

- (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;

} 

该方法panGestureDetected的调用viewDidLoad方式如下:

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

不幸的是,当我尝试移动它时,我的 UIImageView 在屏幕上疯狂地移动。我希望在用户拖动 UIImageView 时看到平滑滚动的 UIImageView 向上/向下。谁能看到我做错了什么?

4

1 回答 1

1

在您的处理程序方法中,只需保留此行并删除所有其他行。

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

您需要在 superView 中获取位置,而不是 imageView 本身。只需更改y中心的值。

于 2013-06-20T16:04:08.937 回答