旋转父视图时,子视图移动到位

时间:2014-12-29 02:04:55

标签: ios objective-c iphone uiview cgaffinetransform

我在此处遵循本指南:http://guti.in/articles/creating-tinder-like-animations/,以便重新创建类似Tinder的刷卡效果。

长话短说,我有一个设有PanGestureRecognizer的UIView,可以执行以下操作:

- (void)dragged:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGFloat xDistance = [gestureRecognizer translationInView:self].x;
    CGFloat yDistance = [gestureRecognizer translationInView:self].y;

    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:{
            self.originalPoint = self.center;
            break;
        };
        case UIGestureRecognizerStateChanged:{
            CGFloat rotationStrength = MIN(xDistance / 320, 1);
            CGFloat rotationAngel = (CGFloat) (2*M_PI * rotationStrength / 16);
            CGFloat scaleStrength = 1 - fabsf(rotationStrength) / 4;
            CGFloat scale = MAX(scaleStrength, 0.93);
            self.center = CGPointMake(self.originalPoint.x + xDistance, self.originalPoint.y + yDistance);
            CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
            CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
            self.transform = scaleTransform;

            break;
        };
        case UIGestureRecognizerStateEnded: {
            [self resetViewPositionAndTransformations];
            break;
        };
        case UIGestureRecognizerStatePossible:break;
        case UIGestureRecognizerStateCancelled:break;
        case UIGestureRecognizerStateFailed:break;
    }
}
- (void)resetViewPositionAndTransformations
{
    [UIView animateWithDuration:0.2
                     animations:^{
        self.center = self.originalPoint;
        self.transform = CGAffineTransformMakeRotation(0);
    }];
}

然而,与示例中的视图不同,我有多个不同的子视图,并且我发现当我应用此代码时,我得到了这个错误的结果:

滑动之前: enter image description here

滑动时: enter image description here

正如您所看到的,似乎子视图是"滑动"不合适,可以这么说。更糟糕的是,当滑动结束时,即使父视图位于正确的位置,子视图仍然看起来略微不合适。如果您有点好奇,我的约束如下: enter image description here

如果有人可以帮助我使用superview正确移动子视图,我会非常感激。我发现最后一个问题的一种方法是存储每个子视图的原始中心并将其恢复到最后,但我确信必须有更好的方法。

编辑:我还没有找到解决办法,但我现在99.9%肯定会对子视图的限制做些事

0 个答案:

没有答案