【问题标题】:UIAttachmentBehavior in UITableViewCellUITableViewCell 中的 UIAttachmentBehavior
【发布时间】:2013-10-19 00:53:48
【问题描述】:

我在我的 tableviewcell 中添加了一个水平 UIPanGestureRecognizer 来平移单元格的子视图,就像 ios7 邮件应用程序显示删除和更多选项的方式一样,这可以按预期工作

- (IBAction)slideCell:(UIPanGestureRecognizer *)sender
{

    CGPoint locationOfPan = [sender locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:locationOfPan];
    TestCell *cell = (TestCell *)[self.tableView cellForRowAtIndexPath:indexPath];    

    if (sender.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [sender translationInView:self.tableView];
        [cell.view setCenter:CGPointMake(cell.view.center.x + translation.x, cell.view.center.y)];
        [sender setTranslation:CGPointMake(0, 0) inView:self.tableView];
    }
}

我还想做的是向我的每个 UITableViewCells 添加一个带有锚点的 UIAttachmentBehavior,这样当我水平平移 cell.view 时,我会感到一些阻力,然后当我放手时,单元格中的视图被平移弹回到它在 x 轴上的原始锚点位置,这可能与 UIDynamics 有关吗?

我还可以将单元格的一侧设置为边界,以防止用户将包含的视图平移到它之外吗?

【问题讨论】:

    标签: ios objective-c uitableview ios7 uikit-dynamics


    【解决方案1】:

    所以这里有代码来让它工作,它仍然需要调整以获得正确的感觉,但一切都按预期工作

    在TestCell UITableViewCell 子类中

    -(void)layoutSubviews{
    
        [super layoutSubviews];
    
        self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
    
        self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.topView attachedToAnchor:self.topView.center];
        [self.attachmentBehavior setFrequency:4.5];
        [self.attachmentBehavior setDamping:0.3];
    
        UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.topView]];
    
        //gravity direction: right
        [gravityBehavior setGravityDirection:CGVectorMake(1.0, 0.0)];
    
        UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.topView]];
        [collisionBehavior addBoundaryWithIdentifier:@"rightBoundary" fromPoint:CGPointMake(320.0f, 0.0f) toPoint:CGPointMake(320.0f, self.contentView.frame.size.height)];
        [self.animator addBehavior:gravityBehavior];
        [self.animator addBehavior:collisionBehavior];
    
    }
    

    在我的 tableViewController 中

    -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
        CGPoint translation = [gestureRecognizer translationInView:self.view.superview];
        // Check for horizontal gesture
        if (fabsf(translation.x) > fabsf(translation.y)) {
            return YES;
        }else{
            return NO;
        }
    
    }
    
    
    - (IBAction)slideCell:(UIPanGestureRecognizer *)sender
    {
    
        CGPoint locationOfPan = [sender locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:locationOfPan];
        TestCell *cell = (TestCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
        if(sender.state == UIGestureRecognizerStateBegan){
    
            [cell.attachmentBehavior setAnchorPoint:cell.topView.center];
            [cell.animator addBehavior:cell.attachmentBehavior];
    
        }
    
        if (sender.state == UIGestureRecognizerStateChanged) {
    
            CGPoint translation = [sender translationInView:self.tableView];
            [cell.attachmentBehavior setAnchorPoint:CGPointMake(cell.topView.center.x + translation.x, cell.topView.center.y)];
            [sender setTranslation:CGPointMake(0, 0) inView:self.tableView];
    
        }
    
        if (sender.state == UIGestureRecognizerStateEnded) {
            //incase pan gesture has moved off the cell
            for(TestCell *cell in [self.tableView visibleCells]){
                [cell.animator removeBehavior:cell.attachmentBehavior];
            } 
    
        }
    
    }
    

    希望这可以帮助其他尝试做同样事情的人,如果有人知道 UIAttachmentBehavior 的设置以获得与 UIScrollview 相同的感觉,请告诉我,谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多