ios整理(七)小應用-重力感應

來源:互聯網
上載者:User

標籤:技術   create   執行個體   執行個體化   round   ios   rand   interval   memory   

重力感應代碼:

#import "ViewController.h"#import <CoreMotion/CoreMotion.h>@interface ViewController ()//建立管理對象 水平儀@property (nonatomic, strong) CMMotionManager *manager;//建立動畫對象@property (nonatomic, strong) UIDynamicAnimator *dyanimat;//重力@property (nonatomic, strong) UIGravityBehavior *gravit;//碰撞@property (nonatomic, strong) UICollisionBehavior *collision;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    }#pragma mark - 執行個體化對象- (CMMotionManager *)manager {    if (_manager == nil) {        _manager = [[CMMotionManager alloc] init];        _manager.deviceMotionUpdateInterval = 0.01;    }    return _manager;}- (UIDynamicAnimator *)dyanimat {    if (_dyanimat == nil) {        _dyanimat = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];    }    return _dyanimat;}- (UIGravityBehavior *)gravit {    if (_gravit == nil) {        _gravit = [[UIGravityBehavior alloc] init];    }    return _gravit;}- (UICollisionBehavior *)collision {    if (_collision == nil) {        _collision = [[UICollisionBehavior alloc] init];        _collision.translatesReferenceBoundsIntoBoundary = YES;    }    return _collision;}#pragma mark - 給對象添加動畫- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    //最多可添加50個    if (self.view.subviews.count >= 50) {        NSLog(@"已到上限");        return;    }    //擷取手指的點    UITouch *touch = touches.anyObject;    CGPoint point = [touch locationInView:self.view];    //建立及切圓角    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];    view.layer.cornerRadius = 10;    view.layer.masksToBounds = YES;    //手指的點就是view的中心點    view.center = point;    //隨機顏色    view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];    [self.view addSubview:view];        //將對象添加到動畫裡    [self.dyanimat addBehavior:self.gravit];    [self.dyanimat addBehavior:self.collision];        // 為view添加重力效果    [self.gravit addItem:view];    // 為view添加碰撞效果    [self.collision addItem:view];        // 開始監聽    [self.manager startDeviceMotionUpdatesToQueue:NSOperationQueue.mainQueue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {        // 設定重力方向        self.gravit.gravityDirection = CGVectorMake(motion.gravity.x, -motion.gravity.y);    }];        //列印添加的控制項的個數    NSLog(@"%zd - %@", self.view.subviews.count, view);    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end   

 

下面是模擬器,正常情況是真機去測試的,因為水平儀模擬器是沒辦法測的。

ios整理(七)小應用-重力感應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.