我有一个程序,我想要有一个图像,可以使用以下选项之一手动旋转:左1度或5度,右1度或5度,或设置旋转到的特定度数。所有的机制都工作得很好,但只要图像旋转到一定程度(而不是0、90、180、270或360度),整个图像就会向下右移。它一直这样做,直到它达到45度角,然后开始移回起始位置,我不知道为什么。我尝试了许多修复方法,但都没有任何效果。
h.file:
//
// 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;
@endm.file:
//
// 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发布于 2014-05-29 17:44:11
首先,一些代码注释。
删除@synthesize代码。不再需要它。属性和iVars应以小写字母开头。
无论如何,从布局代码的缺乏来看,我猜测您使用的是AutoLayout。
在这种情况下,将对图像视图进行约束,以指定...
从顶部到左侧的距离。(或底部或右侧)。
这些工作的方式是,它们采用图像视图的最小封闭(非旋转)矩形,并定位该矩形,使其符合约束。
因为当旋转到45度时,封闭的矩形会变得更大,所以图像看起来像是在移动。
您可以将约束条件更改为如下所示:
将中心与屏幕的中心对齐或类似的东西。
https://stackoverflow.com/questions/23930214
复制相似问题