iOS 技术文档收录iOS开发技术分享程序员

小黄人大眼萌

2017-07-17  本文已影响150人  Renjiee

前面看了卖报的小画家Sure的一篇文章仿新版ofo共享单车小黄人动态效果感觉挺好玩的所以也改写了一个,中途感谢若只如初见_f1af前辈的帮助

先来感受一下👇

小黄人大眼萌.PNG

导入头文件
#import <CoreMotion/CoreMotion.h>


添加小黄人眼珠子

- (void) initEyes{
    //左眼
    EyesImage * leftEye = [[EyesImage alloc] initWithFrame:CGRectMake(CURRENT_SIZE(150), CURRENT_SIZE(300), CURRENT_SIZE(20), CURRENT_SIZE(20))];
    leftEye.image = [UIImage imageNamed:@"minions_eyes"];
    leftEye.layer.masksToBounds = YES;
    leftEye.layer.cornerRadius = 10;
    leftEye.contentMode = UIViewContentModeScaleAspectFit;
    leftEye.collisionBoundsType = UIDynamicItemCollisionBoundsTypeEllipse;
    [self.view addSubview:leftEye];
    self.leftEye = leftEye;
    //右眼
    EyesImage * rightEye = [[EyesImage alloc] initWithFrame:CGRectMake(CURRENT_SIZE(210), CURRENT_SIZE(300), CURRENT_SIZE(20), CURRENT_SIZE(20))];
    rightEye.image = [UIImage imageNamed:@"minions_eyes"];
    rightEye.layer.masksToBounds = YES;
    rightEye.layer.cornerRadius = 10;
    rightEye.contentMode = UIViewContentModeScaleAspectFit;
    rightEye.collisionBoundsType = UIDynamicItemCollisionBoundsTypeEllipse;
    [self.view addSubview:rightEye];
    
    self.rightEye = rightEye;
}

初始化仿真器

- (UIDynamicAnimator *)animator
{
    if (!_animator) {
        // 创建一个物理仿真器
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    return _animator;
}

设置动画和眼珠子活动范围

- (void)SetCoreMotion{
    //  设置重力并纪录
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
    gravity.magnitude = 1;
    [gravity addItem:self.leftEye];
    [gravity addItem:self.rightEye];
    [self.animator addBehavior:gravity];
    self.gravity = gravity;
    
    //设置摩擦力
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
    [collision addItem:self.leftEye];
    [collision addItem:self.rightEye];
    UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[self.leftEye, self.rightEye]];
    //摩擦力
    item.friction = 100;
    [self.animator addBehavior:item];
    // 3.开始仿真
    [self.animator addBehavior:gravity];
    [self.animator addBehavior:collision];
    
    
    //    画出边界
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                          CGRectMake(CURRENT_SIZE(128),CURRENT_SIZE(280), CURRENT_SIZE(50), CURRENT_SIZE(45))];
    
    [collision addBoundaryWithIdentifier:@"circle" forPath:path];
    UIBezierPath *path2 = [UIBezierPath bezierPathWithOvalInRect:
                           CGRectMake(CURRENT_SIZE(186),CURRENT_SIZE(280), CURRENT_SIZE(50), CURRENT_SIZE(45))];
    
    [collision addBoundaryWithIdentifier:@"circle" forPath:path2];
}

开始动画

- (void)SetAccelerometer{
    CMMotionManager *motionManager = [[CMMotionManager alloc]init];
    motionManager.accelerometerUpdateInterval = 0.01; // 告诉manager,更新频率是100Hz
    [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        CMAccelerometerData *newestAccel = motionManager.accelerometerData;
        double accelerationX = newestAccel.acceleration.x;
        double accelerationY = newestAccel.acceleration.y;
        self.gravity.gravityDirection = CGVectorMake(accelerationX, -accelerationY);
    }];
}

完成效果

小黄人大眼萌.gif

代码我已经上传到了GitHub,如果有感兴趣的朋友可以到GitHub中下载--小黄人大眼萌,喜欢的记得给个Stare✨,欢迎大家提出建议,谢谢

我是Renjiee 我要做最骚的程序猿👨‍💻‍

上一篇 下一篇

猜你喜欢

热点阅读