我有一个 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 向上/向下。谁能看到我做错了什么?