#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, assign) BOOL isRuning;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addSubViews];
}
- (void)addSubViews
{
self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(80, 100, 200, 200))];
self.imageView.backgroundColor = [UIColor redColor];
self.imageView.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:self.imageView];
[self.imageView release];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
button1.frame = CGRectMake(200, 400, 40, 50);
button1.backgroundColor = [UIColor blueColor];
[button1 setTitle:@"点" forState:(UIControlStateNormal)];
[button1 addTarget:self action:@selector(actionBlockButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
}
// 点击方法 需求:点击按钮一直转,再点一下停
- (void)actionBlockButton:(UIButton *)button
{
#pragma mark -- 旋转
[UIView animateWithDuration:0.1 animations:^{
// 参数2:旋转角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
} completion:^(BOOL finished) {
[self rotateAnimation];
}];
// 更改转动状态
_isRuning =! _isRuning;
}
// 循环转:把转一次单独写个方法两个转互相调用,实现一直转
- (void)rotateAnimation
{
if (_isRuning == YES) {
[UIView animateWithDuration:0.1 animations:^{
// 旋转
// 参数2:旋转角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4 * 4);
// 给转一个初值
_isRuning = YES;
} completion:^(BOOL finished) {
// 结束的时候调用循环转的方法
[self rotateAnimation];
}];
}
}
// 点击方法
-(void)actionButton:(UIButton *)button
{
// UIView动画:特点:全是类方法调用,开始与结束之间的部分 是动画改变的部分
// 动画开始
// 参数1:可以起一个标识符,参数2:携带的参数
[UIView beginAnimations:@"UIView" context:nil];
// 设置代理
[UIView setAnimationDelegate:self];
// 设置代理放法
[UIView setAnimationWillStartSelector:@selector(WillStart)];
[UIView setAnimationDidStopSelector:@selector(DidStop)];
// 设置动画持续执行动画
[UIView setAnimationBeginsFromCurrentState:YES];
// 动画提交
[UIView commitAnimations];
}
#pragma mark-- 自己设置的代理方法
- (void)WillStart
{
NSLog(@"动画开始");
}
- (void)DidStop
{
NSLog(@"动画结束");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end