0

我有几个 UIImageViews 用户可以拖动、缩放和旋转。当用户导航到另一个视图并返回时,我希望位置、大小和角度保持不变。我如何实现这一目标?

我使用以下代码平移、旋转和缩放

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];

[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
    CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

    [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
    [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}


- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
    [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
    [gestureRecognizer setRotation:0];
}
}


- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
    [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
    [gestureRecognizer setScale:1];
}
}
4

1 回答 1

1

根据您的需要,您可以创建方法,将其作为另一个目标方法添加到所有手势识别器中。

例如

[panGestureRecognizer addTarget: self action: @selector(onGestureRecognizerEnd:)];
[rotationGestureRecognizer addTarget: self action: @selector(onGestureRecognizerEnd:)];
[pinchGestureRecognizer addTarget: self action: @selector(onGestureRecognizerEnd:)];


该方法的示例实现可能是:

- (void)onGestureRecognizerEnd: (UIGestureRecognizer *)gestureRecognizer
{
    switch ( gestureRecognizer.state )
    {
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
        {
            [self _saveViewPosition: gestureRecognizer.view];
            break;
        }
        default:
        {
            break;
        }
    }
}


_saveViewPosition:您可以像这样实现的方法:

- (void)_saveViewPosition: (UIView *)view
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject: [NSKeyedArchiver archivedDataWithRootObject: [NSValue valueWithCGPoint: view.center]]
                 forKey: [self _centerKeyForView: view]];
    [defaults setObject: [NSKeyedArchiver archivedDataWithRootObject: [NSValue valueWithCGAffineTransform: view.transform]]
                 forKey: [self _transformKeyForView: view]];
}


有关更多信息,请查看Xcode 项目

BR

于 2013-04-07T18:27:51.370 回答