1

你好,

我有一个在屏幕上有三个 UIScrollViews 的视图。每当用户摇动 iPhone 时,我想将 UIScrollViews 随机滚动到不同的位置,但我无法完成它。

我在我的 ViewController 类中实现了以下内容来检测和处理摇动手势,这 3 个 UIScrollViews 也属于同一个类。检测到摇晃手势,但 UIScrollViews 没有改变。我究竟做错了什么??

我已经尝试过motionBegan和motionEnded。

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
  int randomTag = arc4random() % [dirContents count];

  CGRect nextImageView = [[scrollView1 viewWithTag:2] frame];
  [scrollView1 scrollRectToVisible:nextImageView animated:YES];

  randomTag = arc4random() % [dirContents count];
  nextImageView = [[scrollView2 viewWithTag:4] frame];
  [scrollView2 scrollRectToVisible:nextImageView animated:YES];

  randomTag = arc4random() % [dirContents count];
  nextImageView = [[scrollView3 viewWithTag:4] frame];
  [scrollView3 scrollRectToVisible:nextImageView animated:YES];
  NSLog(@"Shake Detected End");
    }
}

谢谢你

4

3 回答 3

0

您是否检查了 nextImageView 变量以查看它是否正确?

此外,如果您尝试使用老虎机的动作,我建议您使用 UITableView 而不是自己使用 scrollView

于 2010-08-09T15:14:25.550 回答
0

您是否尝试过使用SetContentOffset而不是scrollRectToVisible尚未?

如果 tableView 中的图像高度相同,则每个“元素”的偏移量始终相同。

[scrollView3 setContentOffset:yourRandomOffsetInPixels animated:YES];

也许这行得通。还要考虑,问题可能是您的抖动检测方法在单独的线程上运行,这意味着您必须motionEnded像这样在主线程上调用您的方法:

[self performSelectorOnMainThread:@selector(motionEnded) withObject:nil waitUntilDone:NO];
于 2010-08-09T15:17:47.353 回答
0

只是一个简单的问题。在您的示例代码中,您生成一个随机标签:

randomTag = arc4random() % [dirContents count];

但是然后您使用特定的标记值(在这种情况下为 4)?我认为当您使用 randomTag 值时它仍然不起作用?你只是在做一些测试?

 nextImageView = [[scrollView2 viewWithTag:4] frame];
于 2010-08-09T15:17:55.143 回答