旋转动画 xcode iphone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14300851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rotation animation xcode iphone
提问by Kurt Lane
Im trying to rotate an image around in a circle using a simple single view application in xcode. I have a circle image on the main storyboard and a button. Im using the following code, but the circle image drifts down to the right then back up to the left as its spinning. I'm not sure what it's missing.
我试图在 xcode 中使用一个简单的单视图应用程序将图像绕一圈旋转。我在主故事板上有一个圆形图像和一个按钮。我使用以下代码,但圆形图像在旋转时向右下移,然后向左移动。我不确定它缺少什么。
Thanks for you help.
谢谢你的帮助。
ViewController.h
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UIImageView *theImageView;
IBOutlet UIButton *theButton;
NSTimer *theTimer;
float angle;
BOOL runStop;
}
@property (atomic, retain) IBOutlet UIImageView *theImageView;
@property (atomic, retain) IBOutlet UIButton *theButton;
-(IBAction)runRoulette:(id)sender;
@end
ViewController.m
视图控制器.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize theButton, theImageView;
- (void)viewDidLoad
{
angle = 0;
runStop = FALSE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)rotateRoulette
{
theImageView.center = CGPointMake(self.theImageView.center.x, self.theImageView.center.y);
theImageView.transform=CGAffineTransformMakeRotation (angle);
angle+=0.001;
}
-(IBAction)runRoulette:(id)sender
{
if(!runStop)
{
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/600.0 target:self selector:@selector(rotateRoulette) userInfo:nil repeats:YES];
}
else
{
[theTimer invalidate];
theTimer = nil;
}
runStop = !runStop;
}
@end
回答by Kurt Lane
I found a very simple way to make my animation work how i wanted it too -
我找到了一种非常简单的方法来让我的动画按照我想要的方式工作 -
theImageView.center = CGPointMake(160.0, 364.0);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];
//theImageView.center=CGPointMake(self.theImageView.center.x, self.theImageView.center.y);
theImageView.transform = CGAffineTransformMakeRotation(M_PI/2.65);
[UIView commitAnimations];
It spins the theImageView M_PI/2.65 (M_PI/2.65 is the roataion amount) for a count of 5.
它旋转 theImageView M_PI/2.65(M_PI/2.65 是旋转量),计数为 5。