2

我有一个 1500x1500 的 uiview,里面有一个 1500x1500 的 uiimageview,在我的 iPad 项目的主视图中居中。当您向任一方向倾斜 iPad 时,下面的代码将无限地左右滚动。我一直在尝试做的是让它也像左右一样无缝地上下滚动。不幸的是,它似乎跳来跳去并且不正确。有任何想法吗?

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

    accelX = acceleration.x;
    accelY = acceleration.y;
    accelZ = acceleration.z;

    int speed;
    speed = 1;

    imageView.center = CGPointMake(imageView.center.x+delta.x,imageView.center.y+delta.y);

    // allows infinite side-to-side scrolling
    if (imageView.center.x > 748){
        imageView.center = CGPointMake(512,384);
        NSLog(@"Greater than 780");
    }
    if (imageView.center.x < 284){
        imageView.center = CGPointMake(519,384);
        NSLog(@"Less than 284");
    }

    if (accelY > 0.01f) {delta.x = -speed;}
    else if (accelY < -0.01f) {delta.x = speed;}
    else if (0.01f < accelY > -0.01f){delta.x = 0;}

// should allow up/down infinite scrolling but does not - it is very jumpy when it resets
//  if (imageView.center.y > 748){imageView.center = CGPointMake(512,384);}
//  if (imageView.center.y < 284){imageView.center = CGPointMake(518,752);}

// this makes it run only with the angle of the iPad the way I want it
//  if (accelZ > -0.5f) {delta.y = speed;}
//  else if (accelZ < -0.1f) {delta.y = -speed;}
//  else if (0.3f < accelZ > -0.3f){delta.y = 0;}

}
4

0 回答 0