1

ios7メールアプリが削除やその他のオプションを表示する方法のように、セルのサブビューをパンするためにtableviewcellに水平UIPanGestureRecognizerを追加しましたが、これは期待どおりに機能します

- (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で行うことができますか?

また、セルの片側を境界として設定して、ユーザーがそれを超えて含まれているビューをパンできないようにすることはできますか?

4

1 に答える 1

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と同じ感覚を得るための設定を知っている人がいたら、教えてください.

于 2013-10-18T16:53:48.773 に答える