摇一摇

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

//UIEventSubtypeMotionShake摇一摇

//如果是摇动事件,可以做一个要当动画展示给用户

//然后请求服务器摇红包的接口,如果摇到了红包,服务器指定接口会返回红包的id

if(motion == UIEventSubtypeMotionShake) {

//点击模拟器的Hardware的Shake Gesture即可实现摇一摇的动作

NSLog(@"shake shake shake");

//需要在Build Phases的link中导入QuartzCore.framework

//核心动画

CABasicAnimation *animation = [CABasicAnimation animation];

//时间

animation.duration= 0.1;

//核心路径transform.rotation.z固定格式

animation.keyPath=@"transform.rotation.z";

//次数

animation.repeatCount= 5;

//自动返回(来回动画)

animation.autoreverses=YES;

//初始状态

animation.fromValue= @(M_PI/12.0f);

//π/12的幅度

//结束状态

animation.toValue= @(-M_PI/12.0f);

//添加开始

[self.imageView.layer addAnimation:animationforKey:@"shake"];

//可移除该动画

//[self.imageView.layer removeAnimationForKey:<#(nonnull NSString *)#>];

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容