Loading UIView transform & center from settings gives a different position

I use pan, a pinch, and UIGestureRecognizers rotation to allow the user to set certain user interface elements exactly where they need them. Using the code here http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more (or similar code here http://mobile.tutsplus.com/tutorials/iphone / uigesturerecognizer / ) both give me what I need so that the user can place these user interface elements as they want.

When the user exits the "UI layout" mode, I save the UIView transformation and center like this:

NSString *transformString = NSStringFromCGAffineTransform(self.transform);
[[NSUserDefaults standardUserDefaults] setObject:transformString forKey:@"UItransform", suffix]];

NSString *centerString = NSStringFromCGPoint(self.center);
[[NSUserDefaults standardUserDefaults] setObject:centerString forKey:@"UIcenter"];

When I restart the application, I read the UIView transform and center like this:

NSString *centerString = [[NSUserDefaults standardUserDefaults] objectForKey:@"UIcenter"];
if( centerString != nil )
    self.center = CGPointFromString(centerString);

NSString *transformString = [[NSUserDefaults standardUserDefaults] objectForKey:@"UItransform"];

if( transformString != nil )
    self.transform = CGAffineTransformFromString(transformString);

And UIVEW ends correctly and scales correctly, but in the wrong place. In addition, after re-entering the "UI layout" mode, I can’t always capture the view with various gestures (as if the displayed view is not a view understood by the gesture recognizer?)

I also have a reset button that sets the UIView transformto the identifier and it centeranytime when it is loaded from the NIB. But after loading the modified UIView center and conversion, even reset does not work. The UIView position is incorrect.

, , center, ( , ). ( , ), UIPanGestureRecognizer, . , , , , . . .

. UIView , , UIView , ?

, - stackoverflow. , .

. , ( , , , , frame.origin).

, / prefs .

NSString *originString = NSStringFromCGPoint(self.frame.origin);
[[NSUserDefaults standardUserDefaults] setObject:originString forKey:@"UIorigin"];

( ):

NSString *originString = [[NSUserDefaults standardUserDefaults] objectForKey:@"UIorigin"];
if( originString ) {
    CGPoint origin = CGPointFromString(originString);
    self.frame = CGRectMake(origin.x, origin.y, self.frame.size.width, self.frame.size.height);
}

( - ) . , , prefs, , , "" UIView ( ), , , ).

№ 2

, frame. Apple http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW6 ( ):

, . frame, , .

№ 3

, , prefs, . bounds rect {{0, 0}, {506, 254}}. VD viewDidLoad - . , , bounds - - . : {{0, 0}, {488.321, 435.981}} ( , , ). reset , , .

reset , , , ! viewDidLoad, bounds - .

EDIT # 4

self.bounds initWithCoder ( NIB), layoutSubviews, self.bounds CGRect. .

. . ( ?) Skram , , .

+5
5

, ! :

, bounds CGRect reset , .

, . - layoutSubviews viewDidLoad. , bounds layoutSubviews.

viewDidLoad. , , :

// UserTransformableView.h
@interface UserTransformableView : UIView {
    CGRect defaultBounds;
}

// UserTransformableView.m
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if( self ) {
        defaultBounds = self.bounds;
    }
    return self;
}

- (void)layoutSubviews {
    self.bounds = defaultBounds;
}
0

frame. NSStringFromCGRect() CGRectFromString().

, . .

, .

UPDATE. Draggable UIViews, . NSCoding , .

//encoding
....
[coder encodeCGRect:self.frame forKey:@"rect"];
// you can save with NSStringFromCGRect(self.frame);
[coder encodeObject:NSStringFromCGAffineTransform(self.transform) forKey:@"savedTransform"];

//init-coder
CGRect frame = [coder decodeCGRectForKey:@"rect"];
// you can use frame = CGRectFromString(/*load string*/);
[self setFrame:frame];
self.transform = CGAffineTransformFromString([coder decodeObjectForKey:@"savedTransform"]);

, . NSStringFromCGRect() CGRectFromString().

2. . - .

[self setFrame:CGRectFromString([[NSUserDefaults standardUserDefaults] valueForKey:@"UIFrame"])];
self.transform = CGAffineTransformFromString([[NSUserDefaults standardUserDefaults] valueForKey:@"transform"]);

, NSUserDefaults UIFrame transform.

+2

. , :

  • , .
  • ,
  • .

:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view1.layer.borderWidth = 5.0;
    view1.layer.borderColor = [UIColor blackColor].CGColor;
    [self.view addSubview:view1];
    view1.center = CGPointMake(150,150);
    view1.transform = CGAffineTransformMakeScale(1.3, 1.3);
    view1.transform = CGAffineTransformRotate(view1.transform, 0.5);

    NSString *savedCentre = NSStringFromCGPoint(view1.center);
    NSString *savedTransform = NSStringFromCGAffineTransform(view1.transform);

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.layer.borderWidth = 2.0;
    view2.layer.borderColor = [UIColor greenColor].CGColor;
    [self.view addSubview:view2];
    view2.center = CGPointFromString(savedCentre);
    view2.transform = CGAffineTransformFromString(savedTransform);

}

:

enter image description here

, , . , , , - , - , , , . .

, , - ! , .

+2

UIView,

CGPoint position = CGPointMake(self.view.origin.x, self.view.origin.y)

NSString _position = NSStringFromCGPoint(position);

// Do the saving
+1

, , , .

1- skram , bounds, , frame. ( , , . , .) IOS, :

transform, undefined . , . , .

2- . , :

  • .
  • .
  • , .

, , . , - , .
:. , . , , ( .)

3- , , , bounds, transform. ( , bounds, frame, frame.) , , , , - .

From the same guide, Apple recommendation:

Usually you change the view transform property when you want to implement an animation. For example, you can use this property to create an animation of your view revolving around its center point. You will not use this property to make permanent changes to your opinion, such as changing its position or size in the representation of the coordinate space in your observations. For this type of change, you must change the frame of the rectangle of your view.

0
source

All Articles