我试图为iOS安装一个简单的指南针。旋转动画似乎围绕屏幕中心旋转指南针图像,所以我试图自动居中指南针图像的UIImageView,以便它可以在不同的设备上工作。
这就是我的问题所在。我试图在ViewDidLoad中使用以下代码使图像居中但没有结果:
compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
我最初将指南针图像放在故事板中的随机位置,以确保其有效。我在这个居中代码之前和之后打印了NSLog检查,以确保它具有效果,并且它似乎确实是图像的中心(在我的iPhone 4s上)但是这没有反映在视图中:
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
我发布图片但不允许,因为这是我的第一篇文章。
事实上,这是整个设置。我确定我在某个地方出了问题:
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
我在这里几乎每个问题和答案都看到了与更改UIImageView的位置和居中有关的任何事情,没有任何事情可以完成。我觉得它可能只是我设置整个程序的方式,这不会是一个惊喜,因为我是一个新的,超级缺乏经验的人。任何帮助都会很棒。甚至建议更好的编程实践。如果你需要更多的信息gimmie喊。
答案 0 :(得分:1)
听起来您可能启用了autolayout并且会覆盖您的代码。
答案 1 :(得分:0)
变化:
compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
致:
compassImage.center = self.view.center;
另外,请确保您对IB中可能会干扰的compassImage没有任何限制。