我有下一个任务:在按钮上单击图像必须旋转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度。但是它从起始中心点向下移动。
另外,如果我再次单击按钮,则什么也不会发生。

最佳答案

我假设您正在使用iOS6和情节提要,请尝试在情节提要中禁用自动布局,然后再次测试动画。
如果可行,并且想要保留自动布局功能,则需要调整约束!

顺便说一句,默认的anchorPoint已经是(0.5, 0.5),该行不是必需的(除非您在其他地方修改了anchorPoint):

[image.layer setAnchorPoint:CGPointMake(0.5,0.5)];

07-25 23:38
查看更多