全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

iOS 仿百度外卖-首页重力感应的实例

今天带来的是仿百度外卖首页的重力感应..(由于只能真机测试,手里测试机只有5s,所以有些地方并没有适配其他机型,需要的还需要根据真机自行适配)

来简单说下实现吧,之前重力感应都是用UIAccelerometer实现的,但是,好像是从iOS 4 以后,这个方法就废弃了,它被直接封装到了CoreMotion框架中,所以现在有关重力感应,加速计什么的都需要通过CoreMotion框架实现,这也算是苹果对于重力感应的整合吧.本文对CoreMotion框架只是进行了简单的使用,想要更深的使用,还是请自行 google(百度上的文档非常少).

好了.下面就是实现代码

(注意这里需要导入系统框架CoreMotion.framework)

// 
// ViewController.m 
// 仿百度外卖首页-重力感应 
// 
// Created by Amydom on 16/12/5. 
// Copyright © 2016年 Amydom. All rights reserved. 
// 
 
#import "ViewController.h" 
#import <CoreMotion/CoreMotion.h> 
 
@interface ViewController ()<UIScrollViewDelegate>{ 
   
  NSTimeInterval updateInterval; 
  CGFloat setx;//scroll的动态偏移量 
   
} 
@property (nonatomic,strong) CMMotionManager *mManager; 
 
@property (nonatomic , strong)UIScrollView *myScrollView; 
 
@property (nonatomic , assign)CGFloat offsetX;//初始偏移量 
 
@property (nonatomic , assign)NSInteger offset; 
 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidAppear:(BOOL)animated_{ 
   
  [super viewDidAppear:animated_]; 
  //在界面已经显示后在调用方法(优化) 
  [self startUpdateAccelerometerResult:0]; 
   
} 
 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor whiteColor]; 
  [self createView]; 
   
} 
 
- (void)createView{ 
   
  //collectionView 
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; 
  UICollectionView *myCollection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout]; 
  myCollection.backgroundColor = [UIColor whiteColor]; 
  [self.view addSubview:myCollection]; 
   
   
   
  _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 22, self.view.frame.size.width, 100)]; 
  _myScrollView.backgroundColor = [UIColor lightGrayColor]; 
  _myScrollView.delegate = self; 
  [self.view addSubview:_myScrollView]; 
   
   
  for (int i = 0; i < 8; i ++) { 
     
    NSString *name = [NSString stringWithFormat:@"%d.jpg",i + 1]; 
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(5 + 885 * i, 10, 80, 80)]; 
    image.image = [UIImage imageNamed:name]; 
    image.backgroundColor = [UIColor orangeColor]; 
    image.layer.masksToBounds = YES; 
    image.layer.cornerRadius = 40; 
    [_myScrollView addSubview:image]; 
    //偏移量为最后 image 的 frame + origin 
    _myScrollView.contentSize = CGSizeMake (image.frame.size.width + image.frame.origin.x, 10); 
     
     
  } 
   
   
 
} 
 
//手指触碰时 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 
   
   _offsetX = scrollView.contentOffset.x; 
  [self stopUpdate]; 
   
} 
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
   
  //优化处理 
  setx = scrollView.contentOffset.x; 
   
  _offset = scrollView.contentOffset.x - _offsetX; 
   
    if (_offset > 0) { 
   
      //left 
   
    }else{ 
   
      //right 
       
    } 
   
   
} 
//手指离开时 
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 
   
  [self startUpdateAccelerometerResult:0]; 
   
} 
 
#pragma mark - 重力感应 
- (CMMotionManager *)mManager 
{ 
  if (!_mManager) { 
    updateInterval = 1.0/15.0; 
    _mManager = [[CMMotionManager alloc] init]; 
  } 
  return _mManager; 
} 
//开始 
- (void)startUpdateAccelerometerResult:(void (^)(NSInteger))result 
{ 
 
  if ([self.mManager isAccelerometerAvailable] == YES) { 
    //回调会一直调用,建议获取到就调用下面的停止方法,需要再重新开始,当然如果需求是实时不间断的话可以等离开页面之后再stop 
    [self.mManager setAccelerometerUpdateInterval:updateInterval]; 
    [self.mManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) 
     { 
       double x = accelerometerData.acceleration.x; 
       double y = accelerometerData.acceleration.y; 
       if (fabs(y) >= fabs(x)) 
       {//前后 
         if (y >= 0){ 
           //Down 
         } 
         else{ 
           //Portrait 
         } 
          
       } else { //左右 
         
         if (x >= 0){ 
            
           setx += 10; 
            
           if (setx <= 360) { 
             //由于以10为单位改变 contentOffset, 会出现顿的现象,加上动画就可解决这个问题 
             [UIView animateWithDuration:0.1 animations:^{ 
                
               _myScrollView.contentOffset = CGPointMake(setx, 0); 
             }]; 
             //模仿 scroll 的回弹效果 
             if (setx == 360) { 
                
               [UIView animateWithDuration:0.5 animations:^{ 
                  
                 _myScrollView.contentOffset = CGPointMake(setx + 50, 0); 
                  
               } completion:^(BOOL finished) { 
                  
                 [UIView animateWithDuration:0.5 animations:^{ 
                    
                   _myScrollView.contentOffset = CGPointMake(setx , 0); 
 
                 }]; 
                  
               }]; 
                
             } 
              
           }else{ 
              
             setx = 360; 
           } 
            
     
         }else{ 
            
           setx -= 10; 
            
           if (setx >= 0) { 
              
             [UIView animateWithDuration:0.1 animations:^{ 
                
               _myScrollView.contentOffset = CGPointMake(setx, 0); 
 
             }]; 
              
             //模仿 scroll 的回弹效果 
             if (setx == 0) { 
                
               [UIView animateWithDuration:0.5 animations:^{ 
                  
                 _myScrollView.contentOffset = CGPointMake(setx - 50, 0); 
                  
               } completion:^(BOOL finished) { 
                  
                 [UIView animateWithDuration:0.5 animations:^{ 
                    
                   _myScrollView.contentOffset = CGPointMake(setx, 0); 
                    
                 }]; 
                  
               }]; 
 
             } 
              
           }else{ 
              
             setx = 0; 
              
           } 
         } 
       } 
     }]; 
  } 
} 
 
//停止感应方法 
- (void)stopUpdate 
{ 
  if ([self.mManager isAccelerometerActive] == YES) 
  { 
    [self.mManager stopAccelerometerUpdates]; 
  } 
} 
//离开页面后停止(移除 mManager) 
- (void)dealloc 
{ 
  //制空,防止野指针 
  _mManager = nil; 
} 
 
 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
 
@end 

到这里,就可以进行真机测试了..

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# iOS  # 仿百度外卖  # ios重力感应代码  # iOS中的NSURLCache数据缓存类用法解析  # Objective-C的缓存框架EGOCache在iOS App开发中的使用  # C++开发在IOS环境下运行的LRUCache缓存功能  # 使用Javascript判断浏览器终端设备(PC、IOS(iphone)、Android)  # iOS 条码及二维码扫描(从相册中读取条形码/二维码)及扫码过程中遇到的坑  # iOS实现时间显示几分钟前  # 几小时前以及刚刚的方法示例  # IOS正则表达式判断输入类型(整理)  # IOS 开发之应用唤起实现原理详解  # IOS TextFiled与TextView 键盘的收起以及处理键盘遮挡  # IOS与网页JS交互详解及实例  # IOS Cache设计详细介绍及简单示例  # 首页  # 的是  # 都是  # 偏移量  # 好了  # 是从  # 这也  # 就可  # 还需要  # 回调  # 解决这个问题  # 大家多多  # 移除  # 进行了  # 就可以  # 再重新  # 量为  # 触碰  # 手里  # 文档 


相关文章: 如何快速辨别茅台真假?关键步骤解析  相册网站制作软件,图片上的网址怎么复制?  专业网站建设制作报价,网页设计制作要考什么证?  移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?  如何用景安虚拟主机手机版绑定域名建站?  简单实现Android验证码  如何快速生成橙子建站落地页链接?  ,制作一个手机app网站要多少钱?  头像制作网站在线制作软件,dw网页背景图像怎么设置?  深圳网站制作的公司有哪些,dido官方网站?  Swift开发中switch语句值绑定模式  微信推文制作网站有哪些,怎么做微信推文,急?  建站之星如何保障用户数据免受黑客入侵?  如何用西部建站助手快速创建专业网站?  建站主机是否等同于虚拟主机?  番禺网站制作公司哪家值得合作,番禺图书馆新馆开放了吗?  制作宣传网站的软件,小红书可以宣传网站吗?  如何快速生成高效建站系统源代码?  建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  内网网站制作软件,内网的网站如何发布到外网?  如何选择CMS系统实现快速建站与SEO优化?  网站建设设计制作营销公司南阳,如何策划设计和建设网站?  商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?  如何挑选高效建站主机与优质域名?  中山网站推广排名,中山信息港登录入口?  建站之星代理费用多少?最新价格详情介绍  c# Task.ConfigureAwait(true) 在什么场景下是必须的  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  小型网站建站如何选择虚拟主机?  PHP 500报错的快速解决方法  平台云上自主建站:模板化设计与智能工具打造高效网站  微信小程序 input输入框控件详解及实例(多种示例)  Bpmn 2.0的XML文件怎么画流程图  黑客入侵网站服务器的常见手法有哪些?  SQL查询语句优化的实用方法总结  如何在IIS7上新建站点并设置安全权限?  如何做静态网页,sublimetext3.0制作静态网页?  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  建站之星如何快速解决建站难题?  c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】  广东企业建站网站优化与SEO营销核心策略指南  如何选择香港主机高效搭建外贸独立站?  建站之星安装模板失败:服务器环境不兼容?  如何选择高性价比服务器搭建个人网站?  全景视频制作网站有哪些,全景图怎么做成网页?  教学网站制作软件,学习*后期制作的网站有哪些?  建站主机类型有哪些?如何正确选型  ,怎么用自己头像做动态表情包?  建站168自助建站系统:快速模板定制与SEO优化指南 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。