陀螺仪,如何限制视图重叠

时间:2019-02-22 06:09:46

标签: ios objective-c core-motion

陀螺仪,如何限制视图的重叠? 我有4个根据陀螺仪传感器移动的视图。我要防止它们彼此重叠。请参阅the video

#import "CMDeviceMotionDemo.h"
@import CoreMotion;

@interface CMDeviceMotionDemo ()

@property (nonatomic, strong) CMMotionManager * motionManager;
@property (weak, nonatomic) IBOutlet UIView *ball;
@property (weak, nonatomic) IBOutlet UIView *ball2;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *allViewObject;


@end

@implementation CMDeviceMotionDemo

#pragma mark Nothing to change here

float X = 0;
float Y = 0;
float R = 120;

float X2 = 50;
float Y2 = 50;
float R2 = 120;


- (void)initBall
{
     self.ball.frame = CGRectMake(300, 300, 120,120);
     self.ball.layer.cornerRadius = self.ball.bounds.size.height / 2;

     self.ball2.frame = CGRectMake(300, 300, 120,120);
     self.ball2.layer.cornerRadius = self.ball2.bounds.size.height / 2;

}

- (void)updateBallWithRoll:(float)roll Pitch:(float)pitch Yaw:(float)yaw accX:(float)accX accY:(float)accY accZ:(float)accZ
{
    // Roll NO : - 1
    X += 2 * roll;
    Y += 2 * pitch;
    X *= 0.8;
    Y *= 0.8;
    CGFloat newX = self.ball.frame.origin.x + X;
    CGFloat newY = self.ball.frame.origin.y + Y;
    newX = fmin(280, fmax(0, newX));
    newY = fmin(527, fmax(64, newY));

    CGFloat newR = R + 10 * accZ;

    X2 += 3 * roll;
    Y2 += 3 * pitch;

    X2 *= 0.6;
    Y2 *= 0.6;

    CGFloat newX2 = self.ball2.frame.origin.x + X2;
    CGFloat newY2 = self.ball2.frame.origin.y + Y2;

    newX2 = fmin(280, fmax(0, newX2));
    newY2 = fmin(527, fmax(64, newY2));

    self.ball.frame = CGRectMake(newX, newY, newR, newR);
    self.ball2.frame = CGRectMake(newX2, newY2, newR, newR);


}

#pragma mark Assignments

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateDeviceMotion) userInfo:nil repeats:YES];
    // TODO: 2.1

    // 2.1 Create a CMMotionManager instance and store it in the property "motionManager"
    self.motionManager = [[CMMotionManager alloc] init];

    // 2.1 Set the motion update interval to 1/60
    self.motionManager.deviceMotionUpdateInterval = 1.0 / 60.0;

    // 2.1 Start updating the motion using the reference frame CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
    [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical];

}


-(void) updateDeviceMotion
{
    // TODO: 2.2

    // 2.2 Get the deviceMotion from motionManager
    CMDeviceMotion *deviceMotion = self.motionManager.deviceMotion;

    // 2.2 Return if the returned CMDeviceMotion object is nil
    if(deviceMotion == nil)
    {
        return;
    }

    // 2.2 Get the attitude from CMDeviceMotion
    CMAttitude *attitude = deviceMotion.attitude;

    // 2.2 Get the userAcceleration from CMDeviceMotion
    CMAcceleration userAcceleration = deviceMotion.userAcceleration;

    // 2.2 Call "updateBallWithRoll:Pitch:Yaw:accX:accY:accZ:" on self with the appropriate arguments

    float roll = attitude.roll;
    float pitch = attitude.pitch;
    float yaw = attitude.yaw;

    float accX = userAcceleration.x;
    float accY = userAcceleration.y;
    float accZ = userAcceleration.z;

    [self updateBallWithRoll:roll Pitch:pitch Yaw:yaw accX:accX accY:accY accZ:accZ ];//accZ

}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    if(self.motionManager != nil){

        // TODO: 2.3

        // 2.3 Stop updating the motionManager
        [self.motionManager stopDeviceMotionUpdates];

        // 2.3 Set the ivar "motionManager" to nil
        self.motionManager = nil;

    }
}
@end

0 个答案:

没有答案