大佬教程收集整理的这篇文章主要介绍了iOS使用加速度计在圆圈内移动对象,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { //NSLog(@"x : %g",acceleration.X); //NSLog(@"y : %g",acceleration.y); //NSLog(@"z : %g",acceleration.z); delta.x = acceleration.x * 10; delta.y = acceleration.y * 10; joypadCap.center = CGPointMake(joypadCap.center.x + delta.x,joypadCap.center.y - delta.y); distance = sqrtf(((joypadCap.center.x - 160) * (joypadCap.center.x - 160)) + ((joypadCap.center.y -206) * (joypadCap.center.y - 206))); //NSLog(@"Distance : %f",distancE); touchAngle = atan2(joypadCap.center.y,joypadCap.center.X); NSLog(@"Angle : %f",touchAnglE); if (distance > 50) { joypadCap.center = CGPointMake(160 - cosf(touchAnglE) * 50,206 - sinf(touchAnglE) * 50); }
我在github上设置了一个示例项目here,但这是最重要的部分:
float distance = sqrtf(((point.x - halfOfWidth) * (point.x - halfOfWidth)) + ((point.y - halfOfWidth) * (point.y - halfOfWidth))); if (distance > maxDistancE) { // Convert point from screen coordinate system to cartesian coordinate system,// with (0,0) located in the centre of the view CGPoint poinTinCartesianCoordSystem = [self convertScreenPointToCartesianCoordSystem:point inFrame:self.view.frame]; // Calculate angle of point in radians from centre of the view CGFloat angle = atan2(poinTinCartesianCoordSystem.y,poinTinCartesianCoordSystem.X); // Get new point on the edge of the circle point = CGPointMake(cos(anglE) * maxDistance,sinf(anglE) * maxDistancE); // Convert BACk to screen coordinate system point = [self convertCartesianPointToScreenCoordSystem:point inFrame:self.view.frame]; }
和:
- (CGPoint)convertScreenPointToCartesianCoordSystem:(CGPoint)point inFrame:(CGRect)frame { float x = point.x - (frame.size.width / 2.0f); float y = (point.y - (frame.size.height / 2.0f)) * -1.0f; return CGPointMake(x,y); } - (CGPoint)convertCartesianPointToScreenCoordSystem:(CGPoint)point inFrame:(CGRect)frame { float x = point.x + (frame.size.width / 2.0f); float y = (point.y * -1.0f) + (frame.size.height / 2.0f); return CGPointMake(x,y); }
以上是大佬教程为你收集整理的iOS使用加速度计在圆圈内移动对象全部内容,希望文章能够帮你解决iOS使用加速度计在圆圈内移动对象所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。