简体   繁体   English

UICollectionViewCell拖动动画

[英]UICollectionViewCell Dragging Animation

Currently I am implementing this to animate the movement of a cell: 目前,我正在实现此功能以动画化单元的运动:

- (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) {
                     }];
}

The issue I am having is whenever I move in the x direction, because my uicollectionview has horizontal scrolling and paging on, it will interrupt my touch events and stop the animation without moving the cell back to place. 我遇到的问题是,无论何时我在x方向上移动,因为uicollectionview具有水平滚动和分页功能,它将中断我的触摸事件并停止动画,而无需将单元格移回原处。

Is there a way to either stop the uicollecitonview from scrolling during my touch event animations or a way to do this so that both can still work while the collection view is scrolling to new cell? 有没有一种方法可以阻止uicollecitonview在我的触摸事件动画期间滚动,或者可以做到这一点,以便当集合视图滚动到新的单元格时两者仍然可以工作?

设置UICollectionView的属性canCancelContentTouches = NO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM