今天带来的是仿百度外卖首页的重力感应..(由于只能真机测试,手里测试机只有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设计详细介绍及简单示例
# 首页
# 的是
# 都是
# 偏移量
# 好了
# 是从
# 这也
# 就可
# 还需要
# 回调
# 解决这个问题
# 大家多多
# 移除
# 进行了
# 就可以
# 再重新
# 量为
# 触碰
# 手里
# 文档
相关文章:
如何规划企业建站流程的关键步骤?
网站制作软件有哪些,制图软件有哪些?
如何快速配置高效服务器建站软件?
如何快速搭建支持数据库操作的智能建站平台?
,在苏州找工作,上哪个网站比较好?
如何解决VPS建站LNMP环境配置常见问题?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
三星网站视频制作教程下载,三星w23网页如何全屏?
建站之星官网登录失败?如何快速解决?
如何使用Golang table-driven基准测试_多组数据测量函数效率
音乐网站服务器如何优化API响应速度?
如何自定义建站之星模板颜色并下载新样式?
*服务器网站为何频现安全漏洞?
建站三合一如何选?哪家性价比更高?
南宁网站建设制作定制,南宁网站建设可以定制吗?
如何续费美橙建站之星域名及服务?
企业宣传片制作网站有哪些,传媒公司怎么找企业宣传片项目?
如何确保FTP站点访问权限与数据传输安全?
定制建站流程步骤详解:一站式方案设计与开发指南
如何用5美元大硬盘VPS安全高效搭建个人网站?
网页设计与网站制作内容,怎样注册网站?
PHP 500报错的快速解决方法
广平建站公司哪家专业可靠?如何选择?
建站之星导航菜单设置与功能模块配置全攻略
c++怎么用jemalloc c++替换默认内存分配器【性能】
企业网站制作公司网页,推荐几家专业的天津网站制作公司?
公司门户网站制作流程,华为官网怎么做?
如何正确下载安装西数主机建站助手?
完全自定义免费建站平台:主题模板在线生成一站式服务
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
建站之星如何实现PC+手机+微信网站五合一建站?
在线制作视频网站免费,都有哪些好的动漫网站?
如何通过服务器快速搭建网站?完整步骤解析
c# 在ASP.NET Core中管理和取消后台任务
孙琪峥织梦建站教程如何优化数据库安全?
建站OpenVZ教程与优化策略:配置指南与性能提升
制作网站的过程怎么写,用凡科建站如何制作自己的网站?
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
如何快速搭建高效WAP手机网站吸引移动用户?
深圳网站制作培训,深圳哪些招聘网站比较好?
php8.4新语法match怎么用_php8.4match表达式替代switch【方法】
实现点击下箭头变上箭头来回切换的两种方法【推荐】
如何用西部建站助手快速创建专业网站?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
TestNG的testng.xml配置文件怎么写
建站之星如何取消后台验证码生成?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
定制建站方案优化指南:企业官网开发与建站费用解析
*请认真填写需求信息,我们会在24小时内与您取得联系。