首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS中的图像视图旋转问题

iOS中的图像视图旋转问题
EN

Stack Overflow用户
提问于 2013-09-14 12:36:40
回答 3查看 1.7K关注 0票数 1

我有下一个任务:点击按钮,图像必须旋转到90度。

我的代码是:

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *vector;
@end

=========

代码语言:javascript
复制
@interface ViewController ()

@end

@implementation ViewController
@synthesize vector;

- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)rotateOn:(id)sender {
    [self rotateImage:self.vector duration:2
                curve:UIViewAnimationCurveEaseIn degrees:M_PI/2];
}

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
              curve:(int)curve degrees:(CGFloat)degrees
{
    // Setup the animation
    [UIView beginAnimations:NULL context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];
    CGAffineTransform transform =
    CGAffineTransformMakeRotation(degrees);
    image.transform = transform;
    [UIView commitAnimations];
}

在点击按钮之前,我有这个

单击此之后

你可以看到图像旋转到90度。但它从起点向下移动。另外,如果我再次单击按钮,什么都不会发生。

EN

回答 3

Stack Overflow用户

发布于 2015-09-30 13:25:59

内部旋转按钮方法使用此代码将图像旋转90。

代码语言:javascript
复制
static int numRot = 0;
myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
++numRot;
票数 1
EN

Stack Overflow用户

发布于 2013-09-14 13:12:35

移除这一行:

代码语言:javascript
复制
  [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];

它实际上不做任何事情,因为{0.5,0.5}是默认的和中心点。您必须使用不同的值(如{0,0} )来获得偏离中心的轮调。

要在每次单击按钮时增加旋转,需要使用累积转换。“制造.”转换是上下文无关的--它们不考虑以前的转换。相反,试着这样做:

代码语言:javascript
复制
CGAffineTransform transform; 
transform = CGAffineTransformRotate(image.transform, degrees);
image.transform = transform;

然后将degrees旋转添加到当前图像的转换中,而不是将转换重置为新值。

票数 0
EN

Stack Overflow用户

发布于 2013-09-14 13:25:16

用途:

代码语言:javascript
复制
imageView.transform = CGAffineTransformMakeRotation(0);//Upright
imageView.transform = CGAffineTransformMakeRotation(M_PI/2);//90 degrees clockwise
imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);//90 degrees counter-clockwise

另外,检查你的帧。

  • 在单击按钮之前
  • 单击按钮后,在旋转之前
  • 后旋转

你可能会在那里发现异常。

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

https://stackoverflow.com/questions/18801817

复制
相关文章

相似问题

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