0

我有一个视图控制器,其中包含一些用户可以平移、旋转和缩放的 UIImageView。位置、角度和大小在发射之间保持不变。这适用于一个 UIImageView。

我可以使用第二个 UIImageView 来平移、旋转和平移。但是,我无法保存此 UIImageView 的位置。当我退出并恢复时,它正在加载另一个 UIImageView 所在的位置。(即两个图像在启动后都加载到相同的位置)。

-createView1:我认为我在and-createView:方法中做错了什么?

这是代码

ViewController.h

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>
@end

ViewController.m

#import <CoreGraphics/CoreGraphics.h>
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSMutableArray *views = [NSMutableArray array];

[views addObject:[self _createView1]];
[views addObject:[self _createView]];
NSLog(@"%@", views);


[UIView animateWithDuration: 0.5 animations: ^
{
    for ( UIView *view in views )
    {
        [self _restoreViewPosition: view];
    }
}];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

pragma mark - 手势识别器处理程序

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

        break;
    }
    default:
    {
        break;
    }
}
}

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

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

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


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


- (void)scalePiece: (UIPinchGestureRecognizer *)gestureRecognizer
{
if ( [gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged )
{
    [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform],
            [gestureRecognizer scale], [gestureRecognizer scale]);
    [gestureRecognizer setScale: 1.0];
}
}

- (void)panPiece1: (UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece1 = [gestureRecognizer view];

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

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


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


- (void)scalePiece1: (UIPinchGestureRecognizer *)gestureRecognizer
{
if ( [gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged )
{
    [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform],
                                                                [gestureRecognizer scale],   [gestureRecognizer scale]);
    [gestureRecognizer setScale: 1.0];
}
}

杂注标记 - UIGestureRecognizerDelegate

- (BOOL) gestureRecognizer: (UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer*)otherGestureRecognizer
{
return YES;
}

杂注标记 - 私有方法

- (void)_restoreViewPosition: (UIView *)view
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

NSData *centerData = [ud objectForKey: [self _centerKeyForView: view]];
if ( centerData )
{
    NSValue *frameValue = [NSKeyedUnarchiver unarchiveObjectWithData: centerData];
    view.center = [frameValue CGPointValue];
}

NSData *transformData = [ud objectForKey: [self _transformKeyForView: view]];
if ( transformData )
{
    NSValue *transformValue = [NSKeyedUnarchiver unarchiveObjectWithData: transformData];
    view.transform = [transformValue CGAffineTransformValue];
}
}

- (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]];

[defaults synchronize];
}

- (NSString *)_transformKeyForView: (UIView *)view
{
return [NSString stringWithFormat: @"view[%d].transform", view.tag];
}

- (NSString *)_centerKeyForView: (UIView *)view
{
return [NSString stringWithFormat: @"view[%d].center", view.tag];
}


- (UIView *)_createView1
{
UIImageView *view1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];

[self.view addSubview:view1] ;
view1.backgroundColor = [UIColor blackColor];
view1.userInteractionEnabled = YES;

view1.frame = ( CGRect ) {
    CGPointMake(0, 0),
    CGSizeMake(10, 16)
};

view1.frame = ( CGRect ) {
    CGPointMake(floorf(( self.view.frame.size.width - view1.frame.size.width ) / 2.0),
                floorf(( self.view.frame.size.height - view1.frame.size.height ) / 2.0)),
    view1.frame.size
};

UIPanGestureRecognizer *panGestureRecognizer1 = [[UIPanGestureRecognizer alloc]
                                                initWithTarget: self action: @selector(panPiece:)];
UIRotationGestureRecognizer *rotationGestureRecognizer1 = [[UIRotationGestureRecognizer alloc]
                                                          initWithTarget: self action: @selector(rotatePiece:)];
UIPinchGestureRecognizer *pinchGestureRecognizer1 = [[UIPinchGestureRecognizer alloc]
                                                    initWithTarget: self action: @selector(scalePiece:)];

//panGestureRecognizer1.delegate = self;
//rotationGestureRecognizer1.delegate = self;
//pinchGestureRecognizer1.delegate = self;

[view1 addGestureRecognizer: panGestureRecognizer1];
[view1 addGestureRecognizer: rotationGestureRecognizer1];
[view1 addGestureRecognizer: pinchGestureRecognizer1];

[panGestureRecognizer1 addTarget: self action: @selector(onGestureRecognizerEnd:)];
[rotationGestureRecognizer1 addTarget: self action: @selector(onGestureRecognizerEnd:)];
[pinchGestureRecognizer1 addTarget: self action: @selector(onGestureRecognizerEnd:)];

[panGestureRecognizer1 release];
[rotationGestureRecognizer1 release];
[pinchGestureRecognizer1 release];

[self.view addSubview: view1];

return view1;
}



- (UIView *)_createView
{
UIImageView *view2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];

[self.view addSubview:view2] ;
view2.backgroundColor = [UIColor blackColor];
view2.userInteractionEnabled = YES;

view2.frame = ( CGRect ) {
    CGPointMake(10, 10),
    CGSizeMake(100, 60)
};

view2.frame = ( CGRect ) {
    CGPointMake(floorf(( self.view.frame.size.width - view2.frame.size.width ) / 2.0),
                floorf(( self.view.frame.size.height - view2.frame.size.height ) / 2.0)),
    view2.frame.size
};

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                initWithTarget: self action: @selector(panPiece1:)];
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]
                                                          initWithTarget: self action: @selector(rotatePiece1:)];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
                                                    initWithTarget: self action: @selector(scalePiece1:)];

//panGestureRecognizer.delegate = self;
//rotationGestureRecognizer.delegate = self;
//pinchGestureRecognizer.delegate = self;

[view2 addGestureRecognizer: panGestureRecognizer];
[view2 addGestureRecognizer: rotationGestureRecognizer];
[view2 addGestureRecognizer: pinchGestureRecognizer];

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

[panGestureRecognizer release];
[rotationGestureRecognizer release];
[pinchGestureRecognizer release];

[self.view addSubview: view2];

return view2;
}

@end
4

1 回答 1

0

-_centerKeyForView:-_transformKeyForView:方法中,您正在生成包含视图标签的键。但是您实际上并没有在任何地方设置视图的标记属性。因此,在创建每个视图后尝试设置不同的标签属性-createView1:-createView:如下所示:

[view1 setTag:0];
...
[view2 setTag:1];
于 2013-04-12T08:28:25.030 回答