我有一个由自定义单元格组成的TableView。此单元格包含一个标签和一个按钮。现在,我在单元格中的按钮中添加了一个panGestureRecognizer 我的问题是,当我尝试开始移动/拖动按钮时,按钮会正确移动,但不幸的是,按钮会隐藏在TableView后面。我该如何让它出现在它前面。自定义单元格是通过编程创建的,TableView也是如此。我的手势处理方法如下:
-(void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer withEvent:(id) event
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
//*******************Dragging Ended*************************************
if([gestureRecognizer state]==UIGestureRecognizerStateEnded)
{
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
//*******************Dragging Started andchanged*************************************
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] ==UIGestureRecognizerStateChanged)
{
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
CGPoint moveLocation=[gestureRecognizer locationInView:piece];
NSIndexPath *indexPath=[tableView indexPathForRowAtPoint:moveLocation];
}
-(void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan || (gestureRecognizer.state == UIGestureRecognizerStateChanged))
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
The code has been modelled based on apple s code here