您的位置:首页 > 产品设计 > UI/UE

ios菜鸟之路:UIView动画之汽车行驶在道路上

2012-10-17 13:29 435 查看
#import "ViewController.h"

#import <QuartzCore/QuartzCore.h>

#define kDuration 0.7
// 动画持续时间(秒)

@interface
ViewController ()

@end

@implementation ViewController

@synthesize scrollView;
@synthesize m_carImage;//5个赛车
@synthesize m_carImage_2;
@synthesize m_carImage_3;
@synthesize m_carImage_4;
@synthesize m_carImage_5;
@synthesize m_backgroundView;//赛道
@synthesize m_backView;
@synthesize m_jiantouView;//路中间的箭头
@synthesize m_jiantouView_left;//路左边的箭头
@synthesize m_jiantouView_right;//路右边的箭头
@synthesize m_matchInfoLabel;
@synthesize m_imageArray;//所有赛车Array
- (void)viewDidLoad
{

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

i_swipeTime=0;

b_panTouched=YES;

i_height=self.view.frame.size.height;

i_width=self.view.frame.size.width;

m_carImage=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"car.png"]];

m_carImage.frame=CGRectMake(i_width/4,
i_height-510,
i_width/2,
i_width/2);

m_carImage.alpha=1.0f;

m_carImage_2=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"car2.png"]];

m_carImage_2.alpha=0.0f;

m_carImage_2.frame=CGRectMake(0,
i_height-510,
i_width/2,
i_width/2);

m_carImage_3=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"car3.png"]];

m_carImage_3.alpha=0.0f;

m_carImage_3.frame=CGRectMake(0,
i_height-510,
i_width/2,
i_width/2);

m_carImage_4=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"car4.png"]];

m_carImage_4.alpha=0.0f;

m_carImage_4.frame=CGRectMake(0,
i_height-510,
i_width/2,
i_width/2);

m_carImage_5=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"car5.png"]];

m_carImage_5.alpha=0.0f;

m_carImage_5.frame=CGRectMake(0,
i_height-510,
i_width/2,
i_width/2);

m_imageArray=[[NSMutableArray
alloc]initWithObjects:m_carImage,m_carImage_2,m_carImage_3,m_carImage_4,m_carImage_5,
nil];

m_backgroundView=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"paodao.png"]];

m_backgroundView.frame=CGRectMake(0,
0, i_width,
200);

m_backView=[[UIView
alloc]initWithFrame:CGRectMake(0,
i_height-290,
i_width,
200)];

m_backView.backgroundColor=[UIColor
greenColor];

m_matchInfoLabel=[[UILabel
alloc]initWithFrame:CGRectMake(90,
160,
i_width/2,
40)];

m_matchInfoLabel.backgroundColor=[UIColor
clearColor];

m_matchInfoLabel.textColor=[UIColor
blackColor];

m_matchInfoLabel.font=[UIFont
systemFontOfSize:12.0f];

m_matchInfoLabel.text=@"跑完这一段,然后继续奔驰";

m_matchInfoLabel.alpha=1.0f;

m_jiantouView=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"jianTou_Center.png"]];

m_jiantouView_left=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"jianTou_left.png"]];

m_jiantouView_right=[[UIImageView
alloc]initWithImage:[UIImage
imageNamed:@"jianTou_right.png"]];

m_jiantouView.alpha=0.0f;

m_jiantouView_left.alpha=0.0f;

m_jiantouView_right.alpha=0.0f;

[self.view
addSubview:m_backView];

[m_backView
addSubview:m_backgroundView];

[m_backView
addSubview:m_matchInfoLabel];

[m_backView
addSubview:m_carImage];

[m_backView
addSubview:m_carImage_2];

[m_backView
addSubview:m_carImage_3];

[m_backView
addSubview:m_carImage_4];

[m_backView
addSubview:m_carImage_5];

[m_backView
addSubview:m_jiantouView_left];

[m_backView
addSubview:m_jiantouView_right];

[m_backView
addSubview:m_jiantouView];

UISwipeGestureRecognizer *recognizer;
recognizer=[[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(recognizerGet:)];

recognizer.direction=UISwipeGestureRecognizerDirectionDown;

[m_backView
addGestureRecognizer:recognizer];

UISwipeGestureRecognizer *recognizer2;
recognizer2=[[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(recognizerGet2:)];

recognizer2.direction=UISwipeGestureRecognizerDirectionUp;
[m_backView
addGestureRecognizer:recognizer2];

}
- (void)viewWillAppear:(BOOL)animated {

[self.navigationController
setNavigationBarHidden:NO
animated:YES];

[super
viewWillAppear:animated];

}

- (void)didReceiveMemoryWarning
{

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}
- (void)recognizerGet:(UISwipeGestureRecognizer *)gesureRecoginer {

if (b_panTouched==NO) {

return;
}

b_panTouched=NO;

[[m_imageArray
objectAtIndex:(i_swipeTime+1)%5]setFrame:CGRectMake((i_width-10)/2,
0, 10,
10)];

[[m_imageArray
objectAtIndex:(i_swipeTime+1)%5]
setAlpha:0.0f];

m_matchInfoLabel.alpha=0.0f;

//[[m_imageArray objectAtIndex:(abs(i_swipeTime)%5)] setAlpha:1.0f];

[UIView
animateWithDuration:0.8
animations:^{

m_matchInfoLabel.alpha=0.0f;

[[m_imageArray
objectAtIndex:i_swipeTime]
setFrame:CGRectMake(-(i_width/2),
400,
i_width*2,
i_width*2)];

[[m_imageArray
objectAtIndex:i_swipeTime]
setAlpha:0.0f];

}
completion:^(BOOL finished) {

m_matchInfoLabel.alpha=1.0f;
}];

m_jiantouView.frame=CGRectMake((i_width-20)/2,
50,
20, 20);

m_jiantouView.alpha=0.2f;

m_jiantouView_left.frame=CGRectMake(60,
20,
20, 20);

m_jiantouView_left.alpha=0.2f;

m_jiantouView_right.frame=CGRectMake(240,
20,
20, 20);

m_jiantouView_right.alpha=0.2f;

[UIView
animateWithDuration:1.2
animations:^{

m_jiantouView.alpha=1.0;

m_jiantouView.frame=CGRectMake((i_width-40)/2,
400, 40,
40);

m_jiantouView_left.alpha=1.0;

m_jiantouView_left.frame=CGRectMake(-210,
300, 40,
40);

m_jiantouView_right.alpha=1.0;

m_jiantouView_right.frame=CGRectMake(i_width+170,
300,
40, 40);

[m_matchInfoLabel
setText:@"跑完这一段,然后必须加油"];

m_matchInfoLabel.alpha=1.0f;

[[m_imageArray
objectAtIndex:(i_swipeTime+1)%5]
setAlpha:1.0f];

[[m_imageArray
objectAtIndex:(i_swipeTime+1)%5]
setFrame:CGRectMake(i_width/4,
i_height-510,
i_width/2,
i_width/2)];

}
completion:^(BOOL finished) {

b_panTouched=YES;

}];

i_swipeTime=(i_swipeTime+1)%5;

}
- (void)recognizerGet2:(UISwipeGestureRecognizer *)gesureRecoginer {

if (b_panTouched==NO) {

return;
}

b_panTouched=NO;

m_jiantouView.alpha=1.0;

m_jiantouView.frame=CGRectMake((i_width-40)/2,
400,
40, 40);

m_jiantouView_left.alpha=1.0;

m_jiantouView_left.frame=CGRectMake(-210,
300,
40, 40);

m_jiantouView_right.alpha=1.0;

m_jiantouView_right.frame=CGRectMake(i_width+170,
300,
40, 40);

m_matchInfoLabel.alpha=0.0f;

// [[m_imageArray objectAtIndex:abs(i_swipeTime)%5] setFrame:CGRectMake(i_width/4, i_height-510, i_width/2, i_width/2)];

[UIView
animateWithDuration:0.8
animations:^{

m_jiantouView.frame=CGRectMake((i_width-20)/2,
50, 20,
20);

m_jiantouView.alpha=0.1f;

m_jiantouView_left.frame=CGRectMake(60,
20, 20,
20);

m_jiantouView_left.alpha=0.1f;

m_jiantouView_right.frame=CGRectMake(240,
20, 20,
20);

m_jiantouView_right.alpha=0.1f;

//m_carImage.transform=CGAffineTransformMakeScale(4.0f, 4.0f);

[[m_imageArray
objectAtIndex:i_swipeTime]
setFrame:CGRectMake((i_width-10)/2,
0, 10,
10)];

[[m_imageArray
objectAtIndex:i_swipeTime]
setAlpha:0.0f];

m_matchInfoLabel.alpha=1.0f;
}
completion:^(BOOL finished) {

m_jiantouView.alpha=0.0f;

m_jiantouView_left.alpha=0.0f;

m_jiantouView_right.alpha=0.0f;
}];

m_matchInfoLabel.alpha=0.0f;

[[m_imageArray
objectAtIndex:((i_swipeTime-1)+5)%5]
setAlpha:0.0f];

[[m_imageArray
objectAtIndex:((i_swipeTime-1)+5)%5]
setFrame:CGRectMake(-(i_width/2),
400,
i_width*2,
i_width*2)];

[UIView
animateWithDuration:1.2
animations:^{

[[m_imageArray
objectAtIndex:((i_swipeTime-1)+5)%5]
setFrame:CGRectMake(i_width/4,
i_height-510,
i_width/2,
i_width/2)];
[[m_imageArray
objectAtIndex:((i_swipeTime-1)+5)%5]
setAlpha:1.0f];

[m_matchInfoLabel
setText:@"跑完这一段,然后必须加油"];

}
completion:^(BOOL finished) {

m_matchInfoLabel.alpha=1.0f;

b_panTouched=YES;
}];

i_swipeTime=((i_swipeTime-1)+5)%5;

NSLog(@"%d",i_swipeTime);

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: