Xcode:当我的图像旋转到非90度角时,它会移动位置

Xcode:当我的图像旋转到非90度角时,它会移动位置,xcode,image,rotation,Xcode,Image,Rotation,我有一个程序,我想有一个图像可以手动旋转使用以下选项之一:左1或5度,右1或5度,或设置一个特定的角度旋转。所有的机制都可以正常工作,但只要图像旋转到一定角度(不是0、90180270或360度),整个图像就会向右下移。它一直这样做,直到它达到45度角,然后开始回到起始位置,我不知道为什么。我尝试了很多修复方法,但没有一个有效 h、 文件: // // AirNavViewController.h // Compass // // Created by JDS on 5/26/14

我有一个程序,我想有一个图像可以手动旋转使用以下选项之一:左1或5度,右1或5度,或设置一个特定的角度旋转。所有的机制都可以正常工作,但只要图像旋转到一定角度(不是0、90180270或360度),整个图像就会向右下移。它一直这样做,直到它达到45度角,然后开始回到起始位置,我不知道为什么。我尝试了很多修复方法,但没有一个有效

h、 文件:

    //
//  AirNavViewController.h
//  Compass
//
//  Created by JDS on 5/26/14.
//  Copyright (c) 2014 THE OHIO STATE UNIVERSITY. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AirNavViewController : UIViewController {


    int Rotation;
    float degrees;


}

@property (weak, nonatomic) IBOutlet UIImageView *Compass;
@property (weak, nonatomic) IBOutlet UILabel *RotationLabel;
@property (weak, nonatomic) IBOutlet UISlider *DegreesSlider;

    -(IBAction)Left5:(id)sender;
    -(IBAction)Left1:(id)sender;
    -(IBAction)Right1:(id)sender;
    -(IBAction)Right5:(id)sender;
    -(IBAction)SliderMoved:(id)sender;
    -(IBAction)CustomRotate:(id)sender;


@end
//
//AirNavViewController.h
//罗盘
//
//由JDS于2014年5月26日创建。
//版权所有(c)2014俄亥俄州立大学。版权所有。
//
#进口
@接口AirNavViewController:UIViewController{
整数旋转;
浮动度;
}
@属性(弱、非原子)IBUIImageView*指南针;
@属性(弱,非原子)IBUILabel*旋转标签;
@性质(弱、非原子)IBUISLIDER*脱脂剂;
-(iAction)Left5:(id)发送方;
-(iAction)Left1:(id)发送方;
-(iAction)Right1:(id)发送方;
-(iAction)Right5:(id)发送方;
-(iAction)滑块移动:(id)发送方;
-(iAction)CustomRotate:(id)发送方;
@结束
m、 文件:

//
//  AirNavViewController.m
//  Compass
//
//  Created by JDS on 5/26/14.
//  Copyright (c) 2014 THE OHIO STATE UNIVERSITY. All rights reserved.
//

#import "AirNavViewController.h"

@interface AirNavViewController ()

@end

@implementation AirNavViewController


    @synthesize Compass, RotationLabel, DegreesSlider;


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

    RotationLabel.text = [NSString stringWithFormat:@"0°"];
}

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


-(IBAction)SliderMoved:(id)sender{

    degrees = DegreesSlider.value;
    RotationLabel.text = [NSString stringWithFormat:@"%.0f°", degrees];

}


-(IBAction)CustomRotate:(id)sender{

    Compass.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);
    Rotation = degrees;

}


-(IBAction)Left5:(id)sender{

    Rotation = Rotation-5;

    if (Rotation > 359) {Rotation = Rotation-360;}
    if (Rotation < 0) {Rotation = Rotation+360;}

    Compass.transform = CGAffineTransformMakeRotation((Rotation*M_PI)/180);

}


-(IBAction)Left1:(id)sender{

    Rotation = Rotation-1;

    if (Rotation > 359) {Rotation = Rotation-360;}
    if (Rotation < 0) {Rotation = Rotation+360;}

    Compass.transform = CGAffineTransformMakeRotation((Rotation*M_PI)/180);

}


-(IBAction)Right1:(id)sender{

    Rotation = Rotation+1;

    if (Rotation > 359) {Rotation = Rotation-360;}
    if (Rotation < 0) {Rotation = Rotation+360;}

    Compass.transform = CGAffineTransformMakeRotation((Rotation*M_PI)/180);
}


-(IBAction)Right5:(id)sender{

    Rotation = Rotation+5;

    if (Rotation > 359) {Rotation = Rotation-360;}
    if (Rotation < 0) {Rotation = Rotation+360;}

    Compass.transform = CGAffineTransformMakeRotation((Rotation*M_PI)/180);
}

@end
//
//AirNavViewController.m
//罗盘
//
//由JDS于2014年5月26日创建。
//版权所有(c)2014俄亥俄州立大学。版权所有。
//
#导入“AirNavViewController.h”
@接口AirNavViewController()
@结束
@AirNavViewController的实现
@综合罗盘、旋转标签、滑环;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
RotationLabel.text=[NSString stringWithFormat:@“0°”];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)滑块移动:(id)发件人{
度数=度数滑块值;
RotationLabel.text=[NSString stringWithFormat:@“%.0f°”,度];
}
-(iAction)自定义旋转:(id)发送方{
Compass.transform=CGAffineTransformMakeRotation(度*M_PI/180);
旋转=度;
}
-(iAction)Left5:(id)发送方{
旋转=旋转-5;
如果(旋转>359){Rotation=Rotation-360;}
如果(旋转<0){旋转=旋转+360;}
Compass.transform=CGAffineTransformMakeRotation((旋转*M_PI)/180);
}
-(iAction)Left1:(id)发送方{
旋转=旋转-1;
如果(旋转>359){Rotation=Rotation-360;}
如果(旋转<0){旋转=旋转+360;}
Compass.transform=CGAffineTransformMakeRotation((旋转*M_PI)/180);
}
-(iAction)Right1:(id)发送方{
旋转=旋转+1;
如果(旋转>359){Rotation=Rotation-360;}
如果(旋转<0){旋转=旋转+360;}
Compass.transform=CGAffineTransformMakeRotation((旋转*M_PI)/180);
}
-(iAction)Right5:(id)发送方{
旋转=旋转+5;
如果(旋转>359){Rotation=Rotation-360;}
如果(旋转<0){旋转=旋转+360;}
Compass.transform=CGAffineTransformMakeRotation((旋转*M_PI)/180);
}
@结束

首先,一些代码注释

删除
@synthetic
代码。它不再需要了。 属性和IVAR应以小写字母开头

无论如何,由于缺少布局代码,我猜您正在使用AutoLayout

在这种情况下,图像视图上的约束将指定

从顶部到左侧的距离。(或底部,或右侧)

它们的工作方式是采用图像视图的最小封闭(非旋转)矩形,并定位该矩形,使其符合约束

因为当旋转45度时,封闭矩形变大,所以图像看起来像是在移动

您可以更改约束,例如


将屏幕中心与屏幕中心对齐或类似的方式。

好的,我明白你的意思,但我如何告诉它这样对齐?好的。您正在使用自动布局。因此,与其对齐“左边缘”或“上边缘”或“前缘”等。。。使用“中心X”和“中心Y”。好的,我找到了对齐工具,对齐了中心坐标,但是这产生了一个新问题。图像现在放大得太大了,但它确实可以正确旋转。我不明白为什么仅仅对齐中心x和y坐标就导致了这种情况。现在需要设置高度和宽度。如果不约束高度和宽度,则图像视图将使用其“固有内容大小”,即放置在其中的图像的高度和宽度。在图像视图中添加高度和宽度约束,应该可以解决您的问题。