トム猫が牛乳を飲む(タイマー、フレームアニメーション)
5349 ワード
1、タイマーを使ってトム猫が牛乳を飲むコードを実現する:
2、フレームアニメーション実現コード:
最後に、効果一覧を添付します.画像が大きすぎて、一部しかアップロードできません.ご了承ください.ありがとうございます.
@interface ZMSecondViewController ()
@end
@implementation ZMSecondViewController
// ,self.view
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//
catView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
catView.image=[UIImage imageNamed:@"drink_0.jpg"];
[self.view addSubview:catView];
// Button
drinkButton=[UIButton buttonWithType:UIButtonTypeSystem];
drinkButton.frame=CGRectMake(240, 290, 60, 60);
[drinkButton addTarget:self action:@selector(drinkClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:drinkButton];
//milk
// , (0,0);
milkView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
milkView.image=[UIImage imageNamed:@"milk.png"];
[drinkButton addSubview:milkView];
//
UIButton *goToFirstButton=[UIButton buttonWithType:UIButtonTypeSystem];
goToFirstButton.frame=CGRectMake(5, 420, 60, 40);
[goToFirstButton setTitle:@"Back" forState:UIControlStateNormal];
[goToFirstButton addTarget:self action:@selector(goBackClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goToFirstButton];
}
//
- (void)drinkClick
{
// button , tom , , ; , ;
//timer , timer ,tom
if(_timer==nil)
{
_timer=[NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(drink) userInfo:nil repeats:YES];
// fire , , , 0.15 changePic ;
// fire , , changePic , , , 0.15 changePic ;
[_timer fire];
// , NSRunLoop
// ,
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
}
// ,
- (void)drink
{
_count++;
NSString *picture=[NSString stringWithFormat:@"drink_%d.jpg",_count];
catView.image=[UIImage imageNamed:picture];
if(_count==80)
{
//_conut 0 ; button ,tom
_count=0;
// ; tom ,_timer ; ,
if(_timer!=nil)
{
// timer, ,timer ,
// ,
[_timer invalidate];
// , button , , timer, tom
_timer =nil;
}
}
}
2、フレームアニメーション実現コード:
@interface ZMSecondViewController ()
@end
@implementation ZMSecondViewController
{
int i;
UIImageView *catView;
NSMutableArray *pictureArray;
}
// ,self.view
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//
catView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
catView.image=[UIImage imageNamed:@"drink_0.jpg"];
[self.view addSubview:catView];
// Button
UIButton *drinkButton=[UIButton buttonWithType:UIButtonTypeSystem];
drinkButton.frame=CGRectMake(240, 290, 60, 60);
[drinkButton addTarget:self action:@selector(drinkClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:drinkButton];
//milk
// , (0,0);
UIImageView *milkView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
milkView.image=[UIImage imageNamed:@"milk.png"];
[drinkButton addSubview:milkView];
//
pictureArray=[[NSMutableArray alloc] init];
for(i=1;i<81;i++)
{
NSString *imageName=[NSString stringWithFormat:@"drink_%d.jpg",i];
UIImage *imageDrink=[UIImage imageNamed:imageName];
[pictureArray addObject:imageDrink];
}
//
UIButton *goToFirstButton=[UIButton buttonWithType:UIButtonTypeSystem];
goToFirstButton.frame=CGRectMake(5, 420, 60, 40);
[goToFirstButton setTitle:@"Back" forState:UIControlStateNormal];
[goToFirstButton addTarget:self action:@selector(goBackClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goToFirstButton];
}
//
- (void)drinkClick
{
NSLog(@" ");
if(i==80)
{
// ; , _fireView
// Timer ,Timer ,
[catView stopAnimating];
i=0;
}
else
{
// ; * 1/30.0 ;
catView.animationDuration=8;
// ; UIImage
catView.animationImages=pictureArray;
// , 0,
catView.animationRepeatCount=1;
//
[catView startAnimating];
}
}
@end
最後に、効果一覧を添付します.画像が大きすぎて、一部しかアップロードできません.ご了承ください.ありがとうございます.