#import "ViewController.h"
@interface ViewController ()
{
UIImageView *imgView;
BOOL flag;
UIImageView *imgView1;
UIImageView *imgView2;
UIImageView *imgView3;
UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
flag = YES;
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(250, 200, 60, 60);
// btn1.hidden = YES;
[self.view addSubview:btn1];
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
imgView1.layer.cornerRadius = 30;
imgView1.layer.masksToBounds = YES;
imgView1.image = [UIImage imageNamed:@"1.png"];
[btn1 addSubview:imgView1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(250, 200, 60, 60);
// btn2.hidden = YES;
[self.view addSubview:btn2];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
imgView2.layer.cornerRadius = 30;
imgView2.layer.masksToBounds = YES;
imgView2.image = [UIImage imageNamed:@"2.png"];
[btn2 addSubview:imgView2];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(250, 200, 60, 60);
// btn3.hidden = YES;
[self.view addSubview:btn3];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
imgView3.layer.cornerRadius = 30;
imgView3.layer.masksToBounds = YES;
imgView3.image = [UIImage imageNamed:@"3.png"];
[btn3 addSubview:imgView3];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(250, 200, 60, 60);
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
imgView.layer.cornerRadius = 30;
imgView.layer.masksToBounds = YES;
imgView.image = [UIImage imageNamed:@"01.png"];
[button addSubview:imgView];
}
-(void)click:(id)sender
{
if (flag)
{
[UIView animateWithDuration:0.5 animations:^{
imgView.transform = CGAffineTransformMakeRotation(3.14);
// [btn1.layer addAnimation:[self moveX:2.0f X:[NSNumber numberWithFloat:-150.0f]] forKey:nil];
// [btn3.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]] forKey:nil];
[btn1.layer addAnimation:[self moveX:1.0 X:[NSNumber numberWithFloat:-150.0f] Direction:0] forKey:nil];
[btn2.layer addAnimation:[self moveX:1.0 X:[NSNumber numberWithFloat:-150.0f] Direction:1] forKey:nil];
// [btn3.layer addAnimation:[self moveX:2.0 X:[NSNumber numberWithFloat:0] Direction:2] forKey:nil];
CGPoint point = CGPointMake(-100, -100);
[btn3.layer addAnimation:[self movepoint:point time:1.0] forKey:nil];
// [btn1.layer addAnimation:[self rotation:2.0 degree:0.78 direction:360 repeatCount:1] forKey:nil];
[UIView animateWithDuration:0.5 animations:^{
btn1.transform = CGAffineTransformMakeRotation(M_PI);
btn2.transform = CGAffineTransformMakeRotation(M_PI);
btn3.transform = CGAffineTransformMakeRotation(M_PI);
}];
}];
flag=NO;
}
else
{
[UIView animateWithDuration:0.5 animations:^{
imgView.transform = CGAffineTransformIdentity;
// [btn1.layer addAnimation:[self moveX:2.0f X:[NSNumber numberWithFloat:0.0f]] forKey:nil];
[btn1.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:0.0f] Direction:0] forKey:nil];
[btn2.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:0.0f] Direction:1] forKey:nil];
CGPoint point = CGPointMake(0, 0);
[btn3.layer addAnimation:[self movepoint:point time:1.0] forKey:nil];
[UIView animateWithDuration:0.5 animations:^{
btn1.transform = CGAffineTransformMakeRotation(0);
btn2.transform = CGAffineTransformMakeRotation(0);
btn3.transform = CGAffineTransformMakeRotation(0);
}];
}];
flag=YES;
}
}
#pragma mark =====横向、纵向移动===========
//横向或者纵向移动
-(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x Direction:(NSUInteger)tag
{
// CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];///.y的话就向下移动。
CABasicAnimation *animation = [[CABasicAnimation alloc] init];///.y的话就向下移动。
if (tag == 0)//横向,改变X
{
animation.keyPath = @"transform.translation.x";
animation.toValue = x;
}
else if (tag == 1)//纵向,改变Y
{
animation.keyPath = @"transform.translation.y";
animation.toValue = x;
}
animation.duration = time;
animation.removedOnCompletion = NO;//yes的话,又返回原位置了。
// animation.repeatCount = MAXFLOAT;
animation.repeatCount = 1;
animation.fillMode = kCAFillModeForwards;
return animation;
}
//移动到某个点
-(CABasicAnimation *)movepoint:(CGPoint )point time:(float)time//点移动
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation"];
animation.toValue=[NSValue valueWithCGPoint:point];
animation.removedOnCompletion=NO;
animation.duration = time;
animation.fillMode=kCAFillModeForwards;
return animation;
}
//旋转
-(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount
{
CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0,direction);
CABasicAnimation* animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];
animation.duration= dur;
animation.autoreverses= NO;
// animation.cumulative= YES;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
animation.repeatCount= repeatCount;
animation.delegate= self;
return animation;
}