欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-18 20:26:16
今天带来的是仿百度外卖首页的重力感应..(由于只能真机测试,手里测试机只有5s,所以有些地方并没有适配其他机型,需要的还需要根据真机自行适配) 来简单说下实现吧,之前...

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

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

来简单说下实现吧,之前重力感应都是用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 

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

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