簡體   English   中英

UITableViewCell中的UIAttachmentBehavior

[英]UIAttachmentBehavior in UITableViewCell

我已經在我的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有關嗎?

我還可以將單元格的一側設置為邊界,以防止用戶平移所包含的視圖嗎?

因此,這里是使此工作正常運行的代碼,仍然需要進行調整以獲得正確的感覺,但是一切都可以按預期進行

在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相同的感覺,請讓我知道,謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM