首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode:当我的图像旋转到非90度的角度时,它会改变位置

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

Stack Overflow用户
提问于 2014-05-29 17:37:14
回答 1查看 268关注 0票数 0

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

h.file:

代码语言:javascript
复制
    //
//  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

m.file:

代码语言:javascript
复制
//
//  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
EN

回答 1

Stack Overflow用户

发布于 2014-05-29 17:44:11

首先,一些代码注释。

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

无论如何,从布局代码的缺乏来看,我猜测您使用的是AutoLayout。

在这种情况下,将对图像视图进行约束,以指定...

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

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

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

您可以将约束条件更改为如下所示:

将中心与屏幕的中心对齐或类似的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23930214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档