IOS开发之——硬件开发-加速计应用实例(04)

一 概述

加速计的作用 :用于 检测设备的运动(比如摇晃)。本文介绍相关的两个示例

  • 控制小球的移动
  • 摇一摇

二 控制小球的移动

2.1 项目描述

  • Storyboard上事先放置一个Ball
  • 随着手机的移动,小球随着上下左右移动
  • 超出边界检测(上下左右边界 ),放置到上下左右边界处

2.2 代码

项目代码
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import "UIView+Extension.h"

@interface ViewController ()
//小球
@property (strong, nonatomic) IBOutlet UIView *imageBall;
//保存速度
@property(nonatomic,assign) CGPoint velocity;
@property(nonatomic,strong) CMMotionManager *mgr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   [self push];
}

//-push方式采集
-(void)push
{
    //1-创建CoreMotion管理者
    //CMMotionManager *mgr=[[CMMotionManager alloc]init];
    self.mgr=[[CMMotionManager alloc]init];
    //2-判断加速计 是否可用
    if (self.mgr.isAccelerometerActive) {
        //3-设置采样时间
        self.mgr.accelerometerUpdateInterval=1/30.0;
        
        //4-开始采样
        [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
            //这个block是采集到数据时就会调用
            if (error) return;
            CMAcceleration acceleration=accelerometerData.acceleration;
            NSLog(@"x=%f,y=%f,z=%f",acceleration.x,acceleration.y,acceleration.z);
            //移动速度
            _velocity.x+=acceleration.x;
            _velocity.y-=acceleration.y;
            
            //移动距离
            self.imageBall.x+=_velocity.x;
            self.imageBall.y+=_velocity.y;
            
            //边界检测
            if (self.imageBall.x<=0) {
                //矫正小球当前的位置
                self.imageBall.x=0;
                //超出了屏幕的左边
                _velocity.x*=-0.5;
            }
            if (self.imageBall.y<=0) {
                //矫正小球当前的位置
                self.imageBall.y=0;
                //超出屏幕的顶部
                _velocity.y*=-0.5;
            }
            if (CGRectGetMaxY(self.imageBall.frame)>=self.view.height) {
                //矫正小球当前的位置
                self.imageBall.y=self.view.height-self.imageBall.height;
                //超出屏幕的底部
                _velocity.y*=-0.5;
            }
            if (CGRectGetMaxX(self.imageBall.frame)>=self.view.width) {
                //矫正小球当前的位置
                self.imageBall.x=self.view.width-self.imageBall.width;
                //超出屏幕的右边
                _velocity.x*=-0.5;
            }
        }];
        
    }else{
        NSLog(@"加速计不可用");
    }
}
@end
UIView+Extension.h
#import <UIKit/UIKit.h>

@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat maxX;
@property (nonatomic, assign) CGFloat maxY;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@end
UIView+Extension.m
#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setMaxX:(CGFloat)maxX
{
    self.x = maxX - self.width;
}


- (CGFloat)maxX
{
    return CGRectGetMaxX(self.frame);
}

- (void)setMaxY:(CGFloat)maxY
{
    self.y = maxY - self.height;
}

- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
//    self.width = size.width;
//    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

@end

2.3 预览

暂无设备,自行测试

三摇一摇

3.1 说明

  • 在AppDelegate中重写:motionBegan(摇晃开始)、motionEnded(摇晃结束)、motionCancelled(摇晃取消)相关方法
  • 安装到手机中,晃动手机,查看方法的执行结果

3.2 代码

#import "AppDelegate.h"

@interface AppDelegate ()
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

#pragma mark -摇一摇
//摇一摇开始
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
    
}
//摇一摇结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
}
//摇一摇取消
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NSString类是iOS开发中非常常用的字符串类,其中substringFromIndex、substringWithRange和substringToIndex是NSString类中的三个常用方法,用于截取字符串的一部分。下面分别介绍它们的使用方法。 1. substringFromIndex方法 该方法用于截取字符串从指定位置到字符串结尾的所有字符,返回截取后的字符串。它的方法签名如下: ``` - (NSString *)substringFromIndex:(NSUInteger)from; ``` 其中,from是一个NSUInteger类型的参数,表示要截取的起始位置,从0开始数。 示例代码: ``` NSString *str = @"Hello World"; NSString *subStr = [str substringFromIndex:6]; NSLog(@"%@", subStr); // 输出:"World" ``` 2. substringWithRange方法 该方法用于截取字符串从指定范围内的所有字符,返回截取后的字符串。它的方法签名如下: ``` - (NSString *)substringWithRange:(NSRange)range; ``` 其中,range是一个NSRange类型的结构体,用来指定要截取的字符串的范围。 示例代码: ``` NSString *str = @"Hello World"; NSRange range = NSMakeRange(6, 5); NSString *subStr = [str substringWithRange:range]; NSLog(@"%@", subStr); // 输出:"World" ``` 3. substringToIndex方法 该方法用于截取字符串从字符串开头到指定位置的所有字符,返回截取后的字符串。它的方法签名如下: ``` - (NSString *)substringToIndex:(NSUInteger)to; ``` 其中,to是一个NSUInteger类型的参数,表示要截取的结束位置,从0开始数。 示例代码: ``` NSString *str = @"Hello World"; NSString *subStr = [str substringToIndex:5]; NSLog(@"%@", subStr); // 输出:"Hello" ``` 以上就是NSString中substringFromIndex、substringWithRange和substringToIndex方法的使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值