首先,一直以为摇一摇的功能实现好高大上,结果百度了。我自己也模仿写了一个demo。主要代码如下:

新建一个项目,名字为AnimationShake。

主要代码:

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

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // User was shaking the device. Post a notification named "shake".
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
    }
}

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

-(void)addAnimations{

AudioServicesPlaySystemSound(_soundID);
    
    //让imgup上下移动
    CABasicAnimation *translation2 = [CABasicAnimation animationWithKeyPath:@"position"];
    translation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    translation2.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 115)];
    translation2.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 40)];
    translation2.duration = 0.4;
    translation2.repeatCount = 1;
    translation2.autoreverses = YES;
    
    //让imagdown上下移动
    CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
    translation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 345+44+20)];
    translation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 420)];
    translation.duration = 0.4;
    translation.repeatCount = 1;
    translation.autoreverses = YES;
    
    [_imgDown.layer addAnimation:translation forKey:@"translation"];
    [_imgUp.layer addAnimation:translation2 forKey:@"translation2"];
}

源码可以去我的github网站:https://github.com/linxiu下载看看。谢谢,欢迎大家指正,我是新手。

最新文章

  1. AFNetworking(AFN)总结
  2. java基础-基本数据类型
  3. .NET Core的“dotnet restore”、“dotnet build”和“dotnet run”命令都是用来干什么的?
  4. HTML标签语义化
  5. uva10943
  6. Java基础知识强化45:StringBuffer类之字符串反转的案例
  7. 移动web性能优化笔记
  8. Springboot基础篇
  9. ThinkPhp 添加模型类
  10. Hibernate-day02
  11. css计算属性 calc()
  12. MVC动态赋值的td选中,获取当前的td的ID或者值
  13. 简单的词法设计——DFA模拟程序
  14. nginx反向代理转发后页面上的js css文件无法加载【原创】
  15. Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程
  16. java exception 01
  17. Delphi XE8 中TIdTCPClient的WriteLn编码变化!
  18. python监控端口脚本[jkport1.0.py]
  19. bionase
  20. JDK 注解详解

热门文章

  1. angularjs onchange
  2. .bat脚本基本命令语法
  3. Erlang 进程被抢占的条件——一个进程长时霸占调度器的极端示例
  4. MongoDB学习笔记——聚合操作之MapReduce
  5. 算法(二)之遗传算法(SGA)
  6. Docker CentOS 7.2镜像systemd问题解决办法
  7. [工具] 分布式系统下批量创建用户及分发公钥打通ssh通道的脚本
  8. docker基础命令详解
  9. mac os利用xampp实现apache下的cgi
  10. UESTC 923 稳住GCD DP + GCD