UIKit Dynamics:UITableViewCell内的附件 [英] UIKit Dynamics: Attachment inside UITableViewCell

查看:105
本文介绍了UIKit Dynamics:UITableViewCell内的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My table view cells contain a circle in an UIView, indicating a value. I want to add the UIKit Dynamics attachment behaviour to that circle in order to for it to lag a bit when scrolling.

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

I don't want to attach the individual cells to each other but only the circle view to the UITableViewCell. The rest of the cell should scroll as usual.

问题: UITableViewCell 的原点始终为(0,0)。如何在滚动时实际移动的视图中添加圆圈?

Problem: The UITableViewCell has its origin always at (0, 0). How can I add the circle to a view that actually does move when scrolling?

推荐答案

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

I finally got it to work. The UITableView moves the coordinate system of every cell and of all views contained within that cell. Therefor I needed to manually move my view inside the UITableViewCell during scrolling while still referring to the initial anchor point.

表视图控制器:

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

这篇关于UIKit Dynamics:UITableViewCell内的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆