#import "infosYYYViewController.h"
@interface infosYYYViewController ()
{
UIImageView * _iViewBird;
}
@end
@implementation infosYYYViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor=[UIColororangeColor];
//设置允许摇一摇功能,一般默认yes
[UIApplication sharedApplication].applicationSupportsShakeToEdit=YES;
//让自己成为第一响应者,如果viewcontroller不是第一响应者无法触发,一般默认是第一响应者
[selfbecomeFirstResponder];
[self createUI];
}
-(void)createUI
{//主画布
UIView * view=[[UIViewalloc]initWithFrame:self.view.bounds];
[self.view addSubview:view];
//加载背影图片
UIImageView * iViewBG=[[UIImageViewalloc]initWithFrame:view.bounds];
iViewBG.image=[UIImageimageNamed:@"back2.jpg"];
[view addSubview:iViewBG];
//创建鸟的动画视图
_iViewBird=[[UIImageViewalloc]initWithFrame:CGRectMake(110,150, 60,48)];
_iViewBird.image=[UIImageimageNamed:@"DOVE 1"];
[view addSubview:_iViewBird];
NSMutableArray * arrayImage=[[NSMutableArrayalloc]init];
for (NSInteger i=1; i<19; i++) {
UIImage * image=[UIImageimageNamed:[NSString stringWithFormat:@"DOVE %i.png",i]];
[arrayImage addObject:image];
}
_iViewBird.animationImages=arrayImage;
_iViewBird.animationDuration=1;
_iViewBird.animationRepeatCount=0;
}
//其实摇一摇也就三个方法,上面完全可以不用实现
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动开始");
[_iViewBirdstartAnimating];
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动取消");
[_iViewBirdstopAnimating];
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//判断是否摇晃结束
if (event.subtype==UIEventSubtypeMotionShake) {
NSLog(@"摇动结束");
//三秒之后触发这个方法,不然看不到效果
[selfperformSelector:@selector(findGirl:)withObject:event afterDelay:3];
}
}
-(void)findGirl:(UIEvent *)event
{
[_iViewBird stopAnimating];
}
@end