user3634400
user3634400

Reputation:

UIImageView not responding to centering

I'm trying to put together a simple compass for iOS. The rotation animation seems to rotate the compass image around the center of the screen so I am trying to automatically center the UIImageView of compass Image so that it will work on different devices.

And this is where my problem is. I have attempted to center the image with the following code within ViewDidLoad but with no result:

compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

I have originally placed the compass image in a random place within the Storyboard just to make sure this works. I have printed NSLog checks before and after this centering code to make sure that it is having and effect and it does seem to be centering the image (on my iPhone 4s) yet this is not reflected in the view:

2014-05-17 12:18:23.015 Compass[447:60b] Old: 160, 386
2014-05-17 12:18:23.019 Compass[447:60b] New: 160, 240

I'd post an image but am not allowed as it is my first post.

In fact heres the whole setup. I'm sure I've gone wrong somewhere:

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet UIImageView *compassImage;
@property (nonatomic, retain) IBOutlet UILabel *trueHeadingLabel;

@end

ViewController.m

#import "ViewController.h"
@interface ViewController()
@end

@implementation ViewController
@synthesize locationManager, compassImage, trueHeadingLabel;


- (void)viewDidLoad
{

    [super viewDidLoad];

    NSLog(@"Old: %i, %i", (int)compassImage.center.x, (int)compassImage.center.y);

    compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

    NSLog(@"New: %i, %i", (int)compassImage.center.x, (int)compassImage.center.y);

    locationManager=[[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.headingFilter = 1;
    locationManager.delegate=self;

    // Start the compass updates
    [locationManager startUpdatingHeading];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{

    // Convert Degree to Radian
    // Multiply by -1 to twist opposite to phone rotation
    float oldRad =  -manager.heading.trueHeading * M_PI / 180.0f;
    float newRad =  -newHeading.trueHeading * M_PI / 180.0f;

    // creating needle spin animation
    CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
    theAnimation.toValue=[NSNumber numberWithFloat:newRad];
    theAnimation.duration = 0.5f;

    // applying the animation
    [compassImage.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
    compassImage.transform = CGAffineTransformMakeRotation(newRad);

    // setting labels to heading
    trueHeadingLabel.text =    [NSString stringWithFormat:@"%i°", (int)(newHeading.trueHeading)];

    // console print of heading
    NSLog(@"True heading: %f", newHeading.trueHeading);
}


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

@end

I have looked at pretty much every question and answer on here with anything to do with changing the position of a UIImageView and centering and nothing is doing the job. I get the feeling that it could just be the way I'm setting up the whole program and this wouldn't be a surprise since I am a new and super-inexperienced. Any help would be fantastic. Even advice on better programming practice too. If you need any more information gimmie a shout.

Upvotes: 0

Views: 102

Answers (2)

user1459524
user1459524

Reputation: 3603

Sounds like you may have autolayout enabled and that overrides your code.

Upvotes: 1

67cherries
67cherries

Reputation: 6951

Change:

compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

To:

compassImage.center = self.view.center;

Also, make sure that you have no constraints on the compassImage in IB that would interfere.

Upvotes: 0

Related Questions