1

我的表格视图单元格包含一个圆圈UIView,表示一个值。我想将UIKit Dynamics附件行为添加到该圆圈,以使其在滚动时稍微滞后。

我不想将单个单元格相互连接,而只想将圆形视图连接到UITableViewCell. 单元格的其余部分应该像往常一样滚动。

问题: 的UITableViewCell起源总是在(0, 0)。如何将圆圈添加到滚动时实际移动的视图中?

4

2 回答 2

1

我终于让它工作了。移动每个单元格以及该UITableView单元格中包含的所有视图的坐标系。因此,我需要UITableViewCell在滚动期间手动移动我的视图,同时仍然参考初始锚点。

表视图控制器:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    BOOL scrollingUp = '\0';
    if (self.lastContentOffset > scrollView.contentOffset.y) {
        scrollingUp = YES;
    }
    else if (self.lastContentOffset < scrollView.contentOffset.y) {
        scrollingUp = NO;
    }

    NSInteger offset = 64; // To compensate for the navigation bar.

    if (scrollingUp) {
        offset = offset - scrollView.contentOffset.y;
    }
    else {
        offset = offset + scrollView.contentOffset.y;
    }

    // Limit the offset so the views will not disappear during fast scrolling.
    if (offset > 10) {
        offset = 10;
    }
    else if (offset < -10) {
        offset = -10;
    }

    // lastContentOffset is an instance variable.
    self.lastContentOffset = scrollView.contentOffset.y;

    for (UITableViewCell *cell in self.tableView.visibleCells) {  
        // Use CoreAnimation to prohibit flicker.          
        [UIView beginAnimations:@"Display notification" context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationBeginsFromCurrentState:YES];

        cell.view.frame = CGRectMake(cell.view.frame.origin.x, offset, cell.view.frame.size.width, cell.view.frame.size.height);
        [UIView commitAnimations];
        [cell.dynamicAnimator updateItemUsingCurrentState:cell.view];
    }
}

表格视图单元格:

-(void)layoutSubviews {
    [super layoutSubviews];

    // _view is the animated UIView.
    UIDynamicItemBehavior *viewBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[_view]];
    viewBehavior.elasticity = 0.9f;

    UIAttachmentBehavior *attachmentBehaviorView = [[UIAttachmentBehavior alloc] initWithItem:_view attachedToAnchor:CGPointMake(_anchorView.frame.origin.x + _anchorView.frame.size.width / 2.0f, _anchorView.frame.origin.y + _anchorView.frame.size.height / 2.0f)];
    attachmentBehaviorView.damping = 8.0f;
    attachmentBehaviorView.frequency = 4.0f;
    attachmentBehaviorView.length = 0.0f;

    [_dynamicAnimator addBehavior:viewBehavior];
    [_dynamicAnimator addBehavior:attachmentBehaviorView];
}
于 2014-01-13T17:56:39.183 回答
0

您可以在anchorPoint期间UIAttachmentBehavior更改-[scrollViewDidScroll:]。您可以参考以下代码片段:

- (void)viewDidLoad
{
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    UIAttachmentBehavior *behavior1 = [[UIAttachmentBehavior alloc] initWithItem:self.circleView
                                          attachedToAnchor:[self tableViewAnchor]];
    behavior1.length = 10.0;
    behavior1.damping = 0.3;
    behavior1.frequency = 2.5;

    [animator addBehavior:behavior1];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    behavior1.anchorPoint = [self.tableView convertPoint:[self tableViewAnchor] toView:self.view];
}

- (CGPoint)tableViewAnchor
{
    return CGPointMake(160.0, 154.0);   // return your target coordination w.r.t. the table view
}

预览:

在此处输入图像描述

于 2013-12-27T18:27:10.937 回答