NSUserDefaults - Saving UIImageView Position

不打扰是莪最后的温柔 提交于 2019-12-11 04:18:34

问题


In my application, I have a UIImageView and a UIButton. The UIImageView has a UIPanGestureRecognizer, and can be dragged around the screen. I have written:

int defaultBallLocation = ball.center; in my - (IBAction)saveButton

and Xcode is saying "Cannot associate CGPoint with int".

So, how can I save the ball (UIImageView) position on the screen by pressing "save", then load the saved position by pressing "load" through NSUserDefaults? I have worked with NSUserDefaults before, but only with a UISegmentedController. Thanks!


回答1:


Another option, you can store it as string:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// Set
[userDefaults setObject:NSStringFromCGPoint(yourPoint) forKey:@"pointPosition"];
// Save
[userDefaults synchronize];
// Get
CGPoint thePoint = CGPointFromString([userDefaults objectForKey:@"pointPosition"]);



回答2:


You are trying to assign an int with a CGPoint struct which is not possible.

To save it into NSUserDefaults you could do:

CGFloat x = ball.center.x;
CGFloat y = ball.center.y;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:x forKey:@"x"];
[defaults setFloat:y forKey:@"y"];


来源:https://stackoverflow.com/questions/17383897/nsuserdefaults-saving-uiimageview-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!