gpt4 book ai didi

ios - UIImageView 不响应居中

转载 作者:行者123 更新时间:2023-11-29 02:57:37 26 4
gpt4 key购买 nike

我正在尝试为 iOS 组装一个简单的指南针。旋转动画似乎围绕屏幕中心旋转罗盘图像,所以我试图自动将罗盘图像的 UIImageView 居中,以便它可以在不同的设备上工作。

这就是我的问题所在。我试图在 ViewDidLoad 中使用以下代码将图像居中,但没有结果:

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

我最初将指南针图像放置在 Storyboard 中的随机位置,以确保其正常工作。我在这个居中代码之前和之后打印了 NSLog 检查,以确保它正在起作用并且它似乎确实使图像居中(在我的 iPhone 4s 上)但这并没有反射(reflect)在 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

我会张贴图片,但不允许,因为这是我的第一篇文章。

事实上,这里是整个设置。我确定我在某处出错了:

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 的位置和居中有关的问题和答案,但没有任何效果。我觉得这可能只是我设置整个程序的方式,这并不奇怪,因为我是一个新手而且经验不足。任何帮助都会很棒。甚至还有关于更好的编程实践的建议。如果您需要更多信息,请大声喊叫。

最佳答案

听起来您可能启用了自动布局并且覆盖了您的代码。

关于ios - UIImageView 不响应居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com