简体   繁体   English

在iPhone上触摸旋转图像

[英]Rotate image on touch for iphone

I am very new to objective C and iPhone app development. 我对目标C和iPhone应用程序开发非常陌生。 I have experience in PHP and java and obviously this language is very different. 我有PHP和Java的经验,显然这种语言是非常不同的。 Right now what I'm trying to do is rotate an arrow when its touched. 现在,我想做的是在触摸箭头时旋转它。

All I need is a method that rotates the image, and then the code (and where it goes) that keeps the touch listener (or whatever its called) in the "main" method (like java) 我需要的是一种旋转图像的方法,然后是将触摸侦听器(或其调用的内容)保留在“ main”方法(如java)中的代码(及其去向)

Sorry if my terminology is wrong. 抱歉,如果我的术语错误。

Thanks! 谢谢!

Well, the easiest way is to apply a transform. 好吧,最简单的方法是应用变换。 Let's say you have an image view called imageView and button target action to apply the transform. 假设您有一个名为imageView的图像视图和一个按钮目标操作来应用转换。 And that handler is called rotateImage:. 该处理程序称为rotateImage :。 The code would look like: 代码如下所示:

 - (IBAction)rotateImage:(id)sender
 {
     self.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
 }

If you are using interface builder you'd just set a target action to the button in your view controller. 如果您使用的是界面构建器,则只需在视图控制器中为按钮设置目标操作即可。

Keep in mind that the rotation transform is in radians, so the above would be a 90deg rotation. 请记住,旋转变换是以弧度为单位的,因此以上将是90度旋转。 You need calculate the correct radians for however many degrees you want to rotate. 您需要为要旋转的度数计算正确的弧度。

If you are adding the button programmatically, you would add the target handler in code with: 如果要以编程方式添加按钮,则可以在代码中使用以下命令添加目标处理程序:

[button addTarget:self action:@selector(rotateImage:) forControlEvents:UIControlEventTouchUpInside];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM