ios - UIKit 动态 : Attachment inside UITableViewCell

标签 ios objective-c uikit-dynamics

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

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

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

最佳答案

我终于让它工作了。 UITableView 移动每个单元格以及该单元格中包含的所有 View 的坐标系。因此,我需要在滚动过程中手动将我的 View 移动到 UITableViewCell 中,同时仍然引用初始 anchor 。

表格 View Controller :

- (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];
    }
}

表格 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];
}

关于ios - UIKit 动态 : Attachment inside UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800738/

相关文章:

ios - 如何暂停和恢复 UIDynamicAnimator 物理模拟

ios - 从其他类的 super View 中删除 View

ios - 如何以 KB 为单位获取 UIImage 的大小?

iphone - iOS 设置 UINavigationBar 标题中按钮的大小

ios - 当 UIGravityBehavior 处于事件状态时,UIDynamicAnimator 拒绝达到平衡

ios - Swiftui 动态动画面板从下到上

iphone - 在 objective-c 中测试

ios、iTunes文件共享保存音频文件

ios - 添加多个 Pod 使 iOS 应用程序启动时间增加 10 秒以上

iOS8后台标准-终止后位置更新