UICollectionViewCell拖动动画

时间:2014-08-04 21:58:48

标签: ios objective-c uicollectionview uicollectionviewcell

目前我正在实施此操作以动画单元格的移动:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event touchesForView:self.contentView] anyObject];
    CGPoint touchLocation = [touch locationInView:self];

    if (CGRectContainsPoint([UIScreen mainScreen].bounds, touchLocation))
    {
        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event touchesForView:self.contentView] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    if (dragging)
    {
        self.frame = CGRectOffset(self.frame, touchLocation.x - oldX, touchLocation.y-oldY);
    }

}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    dragging = NO;
    [UIView animateWithDuration:0.3
                          delay:0.1
         usingSpringWithDamping:0.5
          initialSpringVelocity:1.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.frame = self.origionalFrame;
                     }
                     completion:^(BOOL finished) {
                     }];
}

我遇到的问题是每当我向x方向移动时,因为我的uicollectionview有水平滚动和分页,它会中断我的触摸事件并停止动画而不将单元格移回原位。

有没有办法在触摸事件动画期间停止uicollecitonview滚动或者这样做以便在集合视图滚动到新单元格时两者仍可以工作?

1 个答案:

答案 0 :(得分:1)

设置UICollectionView的属性canCancelContentTouches = NO。