![](/img/trans.png)
[英]UITableViewCell animations stop when UITableView is scrolling
[英]Disable horizontal scrolling on UITableViewCell when UITableView is still scrolling vertically
我在我的UITableViewCell
子類上添加了一個UIPanGestureRecognizer
,這樣當用戶向左滑動時,下面的按鈕就會顯示出來(有點像在Mail應用程序中)。 我在awakeFromNib
這樣做:
// Add a pan gesture recognizer to the pannable view.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panScrollView:)];
panGesture.delegate = self;
[self.pannableView addGestureRecognizer:panGesture];
我想允許表視圖滾動,盡管我的自定義手勢識別器,所以我在同一個UITableViewCell
子類中也有以下內容:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
我現在的問題是我不知道如何一次只允許一個手勢識別器,因此當用戶側掃時,表視圖不會滾動,反之亦然。 救命?
在表視圖的視圖控制器中實現UIGestureRecognizerDelegate,並且除了用於滾動表視圖的垂直手勢識別器之外,還要求任何其他手勢識別器的失敗。
將表視圖的panGestureRecognizer
的委托設置為包含它的視圖控制器(比如它的ContainingViewController
)。
self.tableView.panGestureRecognizer.delegate = self;
Make ContainingViewController
實現UIGestureRecognizerDelegate
。
shouldRequireFailureOfGestureRecognizer:
並返回YES(我假設otherGestureRecognizer
將是水平平移手勢)。 錯誤:'UIScrollView的內置平移手勢識別器必須將其滾動視圖作為其委托。
創建一個bool,允許你的panGesture在panScrollView
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.blockPanGesture = YES;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
self.blockPanGesture = NO;
}
然后
-(void)panScrollView:(UIPanGestureRecognizer *)panGestureRecognizer
{
if(self.blockPanGesture == NO)
{
// do the stuff
}
}
如果您正在平移整個tableview,我會將手勢識別器放在tableView本身上......否則也會
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
它為你處理所有這些...但我假設有一個你不想使用它的原因。 您也可以在此邏輯的基礎上進行構建,例如,您可能希望在scrollView也可以進行調整時進行調整,方法是將相似的檢查放入-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
等。
使用Magoo解決方案,我做了一些適合我的simillar。 希望它可以幫助某人。
在Controller類中
#pragma mark - UIScrollViewDelegate methods
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
ApplicationDelegate.blockPanGesture = YES;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
ApplicationDelegate.blockPanGesture = NO;
}
內部細胞:
- (IBAction)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture
{
if(ApplicationDelegate.blockPanGesture)
return;
// do your stuff
}
僅供參考:在Cell的xib文件中拖動UIPanGestureRecognizer並在IBOutletAction上創建| panGestureRecognizer:|
您是否嘗試將跳出屬性設置為NO?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.