在摇动手势时实用地滚动 UIScrollView

发布于 2024-09-14 05:37:23 字数 1247 浏览 1 评论 0原文

您好,

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

我在 ViewController 类中实现了以下内容来检测和处理摇动手势,3 个 UIScrollView 也属于同一类。检测到摇动手势,但 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");
    }
}

谢谢

HI,

I have a view that has three UIScrollViews on the screen. I want to randomly scroll the UIScrollViews to different positions whenever a user shakes the iPhone, but I am unable to get it done.

I have implemented the following in my ViewController class to detect and handle the shake gesture, the 3 UIScrollViews also belong to the same class. The shake gesture is detected, but the UIScrollViews do not change. What am I doing Wrong??

i have tried both motionBegan and 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");
    }
}

Thank You

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

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

发布评论

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

评论(3

想挽留 2024-09-21 05:37:23

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

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

[scrollView3 setContentOffset:yourRandomOffsetInPixels animated:YES];

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

[self performSelectorOnMainThread:@selector(motionEnded) withObject:nil waitUntilDone:NO];

Have you tried using SetContentOffset instead of scrollRectToVisible yet?

if the images in your tableView are of equal height the offset per "Element" is always the same.

[scrollView3 setContentOffset:yourRandomOffsetInPixels animated:YES];

maybe this works.
also consider, that The Problem might be that your shake-detection Method runs on a separate Thread this would mean that you have to call your motionEnded Method on the mainthread like so:

[self performSelectorOnMainThread:@selector(motionEnded) withObject:nil waitUntilDone:NO];
旧夏天 2024-09-21 05:37:23

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

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

Did you check your nextImageView variable to see if it was correct ?

Further more if you are trying will have the motion of a slot machine, I would recommend you to use UITableView instead of doing it by yourself with scrollView

深爱成瘾 2024-09-21 05:37:23

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

randomTag = arc4random() % [dirContents count];

但随后您使用特定的标签值(在本例中为 4)?我认为当您使用 randomTag 值时它仍然不起作用?你只是在做一些测试?

 nextImageView = [[scrollView2 viewWithTag:4] frame];

Just one quick question. In your example code, you generate a random tag:

randomTag = arc4random() % [dirContents count];

but then you use a specific tag value (4 in this case)? I assume it still doesn't work when you use the randomTag value? and that you were just doing some testing?

 nextImageView = [[scrollView2 viewWithTag:4] frame];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文