最基本的旋转功能代码,
实现功能: 点击按钮开始旋转, 再次点击按钮停止, 再点击又开始旋转, 不点按钮不停止
注释有解释, 复制代码就可以实现功能, 很简单
代码如下:
1 . 声明需要用到的属性
// 声明一个imageView
@property (nonatomic, retain) UIImageView *imageView;
// 声明一个属性 记录是否在旋转
@property (nonatomic, assign) BOOL isRuning;
2 . 创建方法,
- (void)addSubViews
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
self.imageView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.imageView];
[self.imageView release];
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(100, 300, 100, 50);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
先实现旋转功能, 再实现循环旋转功能, 注意要更改旋转状态,
// 点击就旋转,松开就停止
- (void)actionButton:(UIButton *)button
{
// 输入animateWithDuration 提示中 选择托字符多的
[UIView animateWithDuration:0.1 animations:^{
// 设置旋转的角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
} completion:^(BOOL finished) {
// 结束时调用旋转方法
[self rorateAnimatetion];
}];
// 更改转动状态
_isRuning = !_isRuning;
}
// 循环旋转
- (void)rorateAnimatetion
{
if(_isRuning == YES){
[UIView animateWithDuration:0.1 animations:^{
// 设置旋转角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);
} completion:^(BOOL finished) {
// 旋转结束时调用旋转方法
[self rorateAnimatetion];
}];
}
}
3 . 调用方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addSubViews];
}
over