在UITableView上创建一个浮动按钮

时间:2014-03-10 13:18:55

标签: ios objective-c uitableview cocoa-touch layout

我看到iOS floating button over table view关于如何创建浮动按钮。这是我创建按钮的方式。问题是按钮将滚动表格视图。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button]; 

我该如何解决?

我建议你使用https://github.com/gizmoboy7/VCFloatingActionButton这个链接....希望它能为你效用

2 个答案:

答案 0 :(得分:5)

将视图控制器指定为表视图的委托,并覆盖scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]) {
        self.floatingButton.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);     
    }
}

答案 1 :(得分:1)

试试这个:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint origin = [scrollView contentOffset];
    [scrollView setContentOffset:CGPointMake(0.0, origin.y)];

    if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0)
    {
        NSLog(@"Now TableView Scrolling UP");
        // Your Custom Code
    } 
    else 
    {
        NSLog(@"Now TableView Scrolling Down");
        // Your Custom Code
    }
}