ios中摇一摇功能的使用

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值