摇一摇


- (void)viewDidLoad {

    [superviewDidLoad];

   

    // 设置允许摇一摇功能

    [UIApplicationsharedApplication].applicationSupportsShakeToEdit =YES;

    // 并让自己成为第一响应者

    [selfbecomeFirstResponder];

    

    

}


#pragma mark - 摇一摇相关方法

// 摇一摇开始摇动

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"开始摇动");

    //播放声音

    SystemSoundID  soundID;

    NSString *path =@"/System/Library/Audio/UISounds/sms-received1.caf";

    

    AudioServicesCreateSystemSoundID((__bridgeCFURLRef _Nonnull)([NSURLfileURLWithPath:path]), &soundID);

    AudioServicesPlayAlertSound(soundID);

    

    //播放震动

    //AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    return;

}


// 摇一摇取消摇动

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"取消摇动");

    return;

}


// 摇一摇摇动结束

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    if (event.subtype ==UIEventSubtypeMotionShake) {// 判断是否是摇动结束

        NSLog(@"摇动结束");

    }

    return;

}



//创建动画
    CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
    keyAnimaion.keyPath = @"transform.rotation";
    keyAnimaion.values = @[@(-10 / 180.0 * M_PI),@(10 /180.0 * M_PI),@(-10/ 180.0 * M_PI)];//度数转弧度

    keyAnimaion.removedOnCompletion = NO;
    keyAnimaion.fillMode = kCAFillModeForwards;
    keyAnimaion.duration = 0.3;
    keyAnimaion.repeatCount = MAXFLOAT;
    [self.iconImageView.layer addAnimation:keyAnimaion forKey:nil];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值