在摇动手势时实用地滚动 UIScrollView
您好,
我有一个屏幕上有三个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过使用
SetContentOffset
而不是scrollRectToVisible
?如果 tableView 中的图像高度相等,则每个“元素”的偏移量始终相同。
也许这有效。
还要考虑,问题可能是您的抖动检测方法在单独的线程上运行,这意味着您必须在主线程上调用
motionEnded
方法,如下所示:Have you tried using
SetContentOffset
instead ofscrollRectToVisible
yet?if the images in your tableView are of equal height the offset per "Element" is always the same.
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:您是否检查了 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
只是一个简单的问题。在您的示例代码中,您生成一个随机标签:
但随后您使用特定的标签值(在本例中为 4)?我认为当您使用 randomTag 值时它仍然不起作用?你只是在做一些测试?
Just one quick question. In your example code, you generate a random tag:
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?