UIKit Dynamics: Attachment inside UITableViewCell

后端 未结 2 860
梦如初夏
梦如初夏 2021-02-11 09:34

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

2条回答
  •  不知归路
    2021-02-11 10:31

    You can change the anchorPoint of UIAttachmentBehavior during -[scrollViewDidScroll:]. You may refer to the following code snippet:

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

    Preview:

    enter image description here

提交回复
热议问题