iOS 仿微信摇一摇
2016-02-24 14:47
477 查看
iOS 中有摇动要实现的方法:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
我们需要在ViewController的viewDidLoad方法中加入
//可以晃动
[[UIApplication
sharedApplication] setApplicationSupportsShakeToEdit:YES];
//成为第一响应
[self
becomeFirstResponder];
//然后去实现那几个方法就可以了
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
NSLog(@"开始摇动");
//振动效果
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//让upImageView上移
CABasicAnimation *upAnimation = [CABasicAnimation
animationWithKeyPath:@"position.y"];
upAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
upAnimation.fromValue =
@([UIScreen
mainScreen].bounds.size.height/4);
upAnimation.toValue =
@(0);
upAnimation.duration =
1;
upAnimation.repeatCount =
1;
upAnimation.autoreverses =
YES;
[self.upImageView.layer
addAnimation:upAnimation
forKey:@"upAnimation"];
//让downImageView下移
CABasicAnimation *downAnimation = [CABasicAnimation
animationWithKeyPath:@"position.y"];
downAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
downAnimation.fromValue =
@([UIScreen
mainScreen].bounds.size.height/4*3);
downAnimation.toValue =
@([UIScreen
mainScreen].bounds.size.height);
downAnimation.duration =
1;
downAnimation.repeatCount =
1;
downAnimation.autoreverses =
YES;
[self.downImageView.layer
addAnimation:downAnimation
forKey:@"downAnimation"];
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
NSLog(@"取消摇动");
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
NSLog(@"摇动结束");
[self.activity
stopAnimating ];
if (event.subtype ==
UIEventSubtypeMotionShake) {
//something happens
}
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable
UIEvent *)event NS_AVAILABLE_IOS(3_0);
我们需要在ViewController的viewDidLoad方法中加入
//可以晃动
[[UIApplication
sharedApplication] setApplicationSupportsShakeToEdit:YES];
//成为第一响应
[self
becomeFirstResponder];
//然后去实现那几个方法就可以了
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
NSLog(@"开始摇动");
//振动效果
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//让upImageView上移
CABasicAnimation *upAnimation = [CABasicAnimation
animationWithKeyPath:@"position.y"];
upAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
upAnimation.fromValue =
@([UIScreen
mainScreen].bounds.size.height/4);
upAnimation.toValue =
@(0);
upAnimation.duration =
1;
upAnimation.repeatCount =
1;
upAnimation.autoreverses =
YES;
[self.upImageView.layer
addAnimation:upAnimation
forKey:@"upAnimation"];
//让downImageView下移
CABasicAnimation *downAnimation = [CABasicAnimation
animationWithKeyPath:@"position.y"];
downAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
downAnimation.fromValue =
@([UIScreen
mainScreen].bounds.size.height/4*3);
downAnimation.toValue =
@([UIScreen
mainScreen].bounds.size.height);
downAnimation.duration =
1;
downAnimation.repeatCount =
1;
downAnimation.autoreverses =
YES;
[self.downImageView.layer
addAnimation:downAnimation
forKey:@"downAnimation"];
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
NSLog(@"取消摇动");
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
NSLog(@"摇动结束");
[self.activity
stopAnimating ];
if (event.subtype ==
UIEventSubtypeMotionShake) {
//something happens
}
}
相关文章推荐
- 微信h5一键关注的代码
- android 微信支付功能支付返回-1问题
- 微信企业号开发:UserAgent
- iOS 支付 [支付宝 银联 微信]
- 30、微信-表情相关
- 27、微信聊天Fragment_Msg的代码实现
- 26、微信聊天Fragment_Msg的界面布局
- 24、微信-标题栏上的下拉菜单式弹窗TitlePopup
- 23、微信-温馨提示对话框WarnTipDialog
- 22、微信 - 主界面实现
- 21、微信 - 主界面布局
- 微信从无到有
- 如何使用腾讯微信公众平台示例代码…
- Java实现仿微信红包分配规则
- 传统媒体试水微信杂志 联手搭建销售新模式
- 微信红包算法
- 微信支付 系统繁忙
- IOS开发中的微信支付功能
- 微信的那个老外产品经理又写了一篇《中国移动应用设计趋势》
- Java学习:解数独的小程序