单个UIImageViews不会在各自的位置保存

时间:2013-04-12 06:24:35

标签: ios objective-c uiimageview

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

我可以使用第二个UIImageView来平移,旋转和翻译。但是,我无法保存此UIImageView的位置。当我退出并恢复时,它将加载到其他UIImageView所在的位置。 (即两张图像在发布后都被加载到同一位置)。

我认为我在-createView1:-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];
}
}

pragma mark - UIGestureRecognizerDelegate

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

pragma mark - 私有方法

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

1 个答案:

答案 0 :(得分:0)

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

[view1 setTag:0];
...
[view2 setTag:1];