我有下一个任务:点击按钮,图像必须旋转到90度。
我的代码是:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *vector;
@end=========
@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度。但它从起点向下移动。另外,如果我再次单击按钮,什么都不会发生。
发布于 2015-09-30 13:25:59
内部旋转按钮方法使用此代码将图像旋转90。
static int numRot = 0;
myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
++numRot;发布于 2013-09-14 13:12:35
移除这一行:
[image.layer setAnchorPoint:CGPointMake(0.5,0.5)];它实际上不做任何事情,因为{0.5,0.5}是默认的和中心点。您必须使用不同的值(如{0,0} )来获得偏离中心的轮调。
要在每次单击按钮时增加旋转,需要使用累积转换。“制造.”转换是上下文无关的--它们不考虑以前的转换。相反,试着这样做:
CGAffineTransform transform;
transform = CGAffineTransformRotate(image.transform, degrees);
image.transform = transform;然后将degrees旋转添加到当前图像的转换中,而不是将转换重置为新值。
发布于 2013-09-14 13:25:16
用途:
imageView.transform = CGAffineTransformMakeRotation(0);//Upright
imageView.transform = CGAffineTransformMakeRotation(M_PI/2);//90 degrees clockwise
imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);//90 degrees counter-clockwise另外,检查你的帧。
你可能会在那里发现异常。
https://stackoverflow.com/questions/18801817
复制相似问题