玩转iOS陀螺仪、加速度计与相机功能
一、利用加速度计控制小球移动
在iOS开发中,我们可以借助加速度计和陀螺仪实现一些有趣的交互效果,比如控制小球在屏幕上移动。
- 小球移动的计算逻辑
- 初始化 :当从nib文件加载视图时,会调用
initWithCoder:方法进行额外的初始化操作。首先加载ball.png图片,然后计算视图的中心位置作为小球的起始点,并将两个轴的速度都设置为0。
- 初始化 :当从nib文件加载视图时,会调用
self.image = [UIImage imageNamed:@"ball.png"];
self.currentPoint = CGPointMake((self.bounds.size.width / 2.0f) + (image.size.width / 2.0f), (self.bounds.size.height / 2.0f) + (image.size.height / 2.0f));
ballXVelocity = 0.0f;
ballYVelocity = 0.0f;
- **绘制图像**:`drawRect:`方法非常简单,只需在`currentPoint`存储的位置绘制在`initWithCoder:`中加载的图像。
- **设置当前点**:`setCurrentPoint:`方法会先将旧的`currentPoint`值存储在`previousP
超级会员免费看
订阅专栏 解锁全文
1204

被折叠的 条评论
为什么被折叠?



