我正在尝试将 UISwipeGestureRecognizer 添加到位于我的 UIViewController 上的 UIImageView,如果 UIImageView 的框架设置为屏幕边界,它工作正常。我需要更小的 UIImageView,当我将框架设置为适当的尺寸时,它不再拾取水平滑动。我怎样才能让它在小于全屏尺寸的任何东西上工作?
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"Star-Rating-Stroke.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds]; // THIS WORKS !!!
// imageView.frame = CGRectMake(36,70,33,33); // THIS DOESN'T !!!
[imageView setUserInteractionEnabled:YES];
[self.view addSubview:imageView];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeLeft];
[imageView addGestureRecognizer:swipeRight];
}
- (IBAction) handleRightSwipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"Right Swipe");
}
- (IBAction) handleLeftSwipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"Left Swipe");
}
最佳答案
如果它在 ImageView 全屏时有效,那么它也适用于(一些)较小的帧。您应该通过逐渐减小 ImageView 的大小来进行试验,以查看您开始出现问题的确切位置。
无论如何,像您正在使用的那样非常小的尺寸非常有意义。 UISwipeGestureRecognizer 类将有一些内部测量,用于确定手指是否已在指定方向上平移足够远以识别手势。如果您的 ImageView 的框架小于此(未知)值,则手势将始终失败。
作为替代方法,我建议您创建自己的 UIGestureRecognizer 子类,它的行为类似于滑动手势,但需要较少的移动来检测。如果这不是您感兴趣的事情,您可以考虑向 ImageView 的 super View 添加手势并跟踪触摸事件的位置以确定它们是否发生在 ImageView 的范围内。
关于ios - 水平 UISwipeGestureRecognizer 不适用于 Small UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182237/