在我的例子中,我有 UIView 和 UITableView Controller 。我想要的是通过滑动 UIView 来滚动 TableView 。我为 UIView 添加了向上滑动和向下滑动手势。当我在此 View 上滑动时,沿着滑动方向滚动表格 View 。可能吗?
最佳答案
从您的 viewDidLoad 调用此方法。 此方法将禁用 tableview 的滚动以防止手动滚动。假设您有 100 行高度为 30 并且 myview 是必须执行滚动的 View 。 myview添加滑动手势,设置滑动方向很重要
-(void)initialSetup {
[self.tableView setScrollEnabled:NO];
int numberOfRows=100;
int rowHeight=30;
myview=[[UIView alloc]initWithFrame:CGRectMake(280, 0, 30, numberOfRows*rowHeight)];
[myview setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:myview];
UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[myview addGestureRecognizer:swipeDown];
UISwipeGestureRecognizer *swipeUp=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(motion:)];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[myview addGestureRecognizer:swipeUp];}
当滑动手势发生时,无论是向上滑动还是向下滑动,都会调用下面的方法。 当该方法接收到 swipeUp 手势时,它会将 tableview 中最后一个当前可见的行滚动到顶部 当该方法收到 swipeDown 时,它会将当前可见的第一行滚动到底部
-(void)motion:(UISwipeGestureRecognizer *)发件人 {
if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
[self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:[self.tableView.indexPathsForVisibleRows count]-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}else if(sender.direction==UISwipeGestureRecognizerDirectionDown)
{
[self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:0 ] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
关于iPhone 通过向外滑动表格 View 来滚动 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14454875/