Iphone SDK:使用加速度计移动图像

发布于 2024-11-06 05:01:30 字数 349 浏览 1 评论 0原文

我尝试通过以下方式用加速度计移动图像:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    image.center = CGPointMake(acceleration.x, acceleration.y);

}

当我测试应用程序时,应该移动的图像只是位于 x0 y0 位置。

我声明了加速度计,称为 .h UIAccelerometerDelegate 等等...

我做错了什么?

提前致谢! -DD

Im trying to move an image around with the accelerometer by doing that:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

    image.center = CGPointMake(acceleration.x, acceleration.y);

}

When i test the app, the image that is supposed to move around just sits in the x0 y0 position.

I declared the accelerometer, called the .h UIAccelerometerDelegate and so on...

What am i doing wrong?

Thanks in advance! -DD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-11-13 05:01:30

您是否意识到加速度计返回的是加速度测量值而不是显示屏上的点?无论如何,您需要做的是改变中心(而不是完全替换它),这将允许您移动图像。

沿着这些思路:

image.center = CGPointMake(image.center.x + acceleration.x, 
                           image.center.y - acceleration.y);

还需要注意的是,加速度通常保持在 -1 和 1 之间(除非用户摇动设备),这是由于重力为 1G。因此,您可能应该将 Acceleration.x 和 .y 值乘以某个常数,以使图像每次移动的速度比大约 1 点快一些。

您还应该考虑其他一些事情,如果图像位于屏幕边缘怎么办?如果用户想要在平面上以外的其他位置使用应用程序(需要校准加速度计)怎么办?

You do realize that the accelerometer returns, as the name would suggest, measures of acceleration not points on the display? Anyway, what you need to do, is alter the center (not replace it completely), which will allow you to move the image.

Something along these lines:

image.center = CGPointMake(image.center.x + acceleration.x, 
                           image.center.y - acceleration.y);

It is also important to note that the acceleration usually stays between -1 and 1 (unless the user shakes the device), which is due to the gravity being 1G. Therefore you should probably multiply the acceleration.x and .y values with some constant to make the image move a bit faster than about 1 point at a time.

There are additional things you should think about, what if the image is at the edge of the screen? What if the user wants to use the app in some other position than flat on a surface (needs calibration of the accelerometer)?

是你 2024-11-13 05:01:30
-(void)moveImage:(id)sender 
{
    [operationView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];

    [[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
    {
    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
        [imgDeleteView setHidden:FALSE];
    }
    else if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {
        [imgDeleteView setHidden:TRUE];
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [[(UIPanGestureRecognizer *)sender view] setCenter:translatedPoint];
 }
-(void)moveImage:(id)sender 
{
    [operationView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];

    [[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
    {
    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
        [imgDeleteView setHidden:FALSE];
    }
    else if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {
        [imgDeleteView setHidden:TRUE];
    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [[(UIPanGestureRecognizer *)sender view] setCenter:translatedPoint];
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文