Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:05:56+00:00 2026-06-03T19:05:56+00:00

I am moving images around in my app which is working fine. The problem

  • 0

I am moving images around in my app which is working fine. The problem is that I’d like to keep the images in place when the user comes back to that view. At present, I am using a string to hold the image centre:

NSString *centre = NSStringFromCGPoint(image.center);

I have an NSUserDefault variable called theCentre:

theCentre = [NSUserDefaults standardUserDefaults];

which I try to save using:

[theCentre setObject:centre forKey:@"centre"];

When I output the log, I get the following:

{280, 320}

which is great.
In the viewDidLoad, I have tried the exact same log and it is returning (null). I take it that this means it isn’t saving theCentre properly. Am I missing something fundamental here because I have used NSUserDefaults elsewhere in my app and it has been working ok. Many thanks in advance.
Edit: this is the whole code to try and aid debugging 🙂

-(void)getBounds
{
lowerBoundX = 250;
upperBoundX = 310;
lowerBoundY = 290;
upperBoundY = 350;

}

#pragma mark -
#pragma mark UIPanGestureRecognizer selector

- (void) dragGesture:(UIPanGestureRecognizer *) panGesture
{
 [self getBounds];

CGPoint translation = [panGesture translationInView:self.view];

switch (panGesture.state) {
    case UIGestureRecognizerStateBegan:{
        originalCentre = dragImage.center;
    }
        break;
    case UIGestureRecognizerStateChanged:{
        dragImage.center = CGPointMake(dragImage.center.x + translation.x,
                                       dragImage.center.y + translation.y);

    }
        break;
    case UIGestureRecognizerStateEnded:{            
        if (((dragImage.center.x >= lowerBoundX) && (dragImage.center.y >= lowerBoundY) &&
            (dragImage.center.x <= upperBoundX) && (dragImage.center.y <= upperBoundY))) {

            dragImage.center = CGPointMake(280, 320);
            [dragImage setUserInteractionEnabled:NO];

            theCentre = [NSUserDefaults standardUserDefaults];

        NSString *centre = NSStringFromCGPoint(dragImage.center);

            [theCentre setObject:centre forKey:@"centre"];

            NSLog(@"%@", [theCentre objectForKey:@"centre"]);
            [theCentre synchronize];

            break;
        }
        [UIView animateWithDuration:0.5 
                         animations:^{
                             dragImage.center = originalCentre;
                         }
                         completion:^(BOOL finished){
                         }];
    }
        break;
    default:
        break;
}

[panGesture setTranslation:CGPointZero inView:self.view];
}

-(IBAction)clear{
[UIImageView animateWithDuration:0.5 animations:^{
dragImage.center = CGPointMake(100, 200);
}];

[dragImage setUserInteractionEnabled:YES];
}

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

NSLog(@"%@", [theCentre objectForKey:@"centre"]);

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragGesture:)];

[dragImage addGestureRecognizer:panGesture];

[dragImage setUserInteractionEnabled:YES];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:06:06+00:00Added an answer on June 3, 2026 at 7:06 pm

    Data saved in NSUserDefaults is first cached and thus it is not available instantly from other instances of the NSUserDefaults. The data is saved periodically on some predefined events/time intervals. However you can force a data synchronization by calling synchronize like this:

    NSString *theCenter = NSStringFromCGPoint(image.center);
    theCenter = [NSUserDefaults standardUserDefaults];
    [theCenter setObject:centre forKey:@"center"];
    [theCenter synchronize];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project which involves moving products around a virtual living
I have three images that I want the user to move around the screen.
I'm working on an IPhone robot that would be moving around. One of the
I'm working on an augmented reality app for iPhone that involves a very processor-intensive
I'm trying to add GIF generation functionality to my app so that the user
I'm dragging an imageView around the screen which is working great. At the moment,
i am considering moving my images to a subdomain on my website, and i
When moving to Spring 2.5.x I found that it adds more stereotype annotations (on
Im moving from django to pylons, is there an admin app?
I am moving a system from a VB/Access app to SQL server. One common

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.