簡體   English   中英

水平UISwipeGestureRecognizer對Small UIImageView不起作用

[英]Horizontal UISwipeGestureRecognizer doesn't work for Small UIImageView

我試圖將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");
}

如果在全屏顯示圖像時可以使用,那么它也可以使用(某些)較小的幀。 您應該通過逐漸減小圖像視圖的大小來進行試驗,以查看確切的位置開始出現問題。

無論如何,以非常小的尺寸(例如您正在使用的尺寸)非常合理。 UISwipeGestureRecognizer類將具有一些內部度量,該度量用於確定手指是否已沿指定方向平移足夠遠以識別手勢。 如果圖像視圖的邊框小於此(未知)值,則手勢將始終失敗。

作為一種替代方法,我建議您要么創建自己的UIGestureRecognizer子類,其作用就像滑動手勢,但需要較少的移動來進行檢測。 如果您對此不感興趣,可以考慮向圖像視圖的超級視圖添加手勢,並跟蹤觸摸事件的位置,以確定它們是否在圖像視圖的范圍內發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM