簡體   English   中英

子類化的UIScrollView可檢測按鈕內的觸摸,但對用戶交互無響應

[英]Subclassed UIScrollView detects touches within buttons but is otherwise unresponsive to user interaction

我有一個垂直的UIScrollView,它具有幾個大按鈕,占據了滾動視圖中的大部分空間。 出於這個原因,我必須在此ios 8之后將滾動視圖子類化-水平滾動視圖中的按鈕攔截pan事件-滾動不起作用 )並覆蓋以下類方法:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    static int count = 0;
    count++;
    NSLog(@"calling this function 2");
    NSLog(@"count = %d", count);
    UITouch *touch = [touches anyObject];

    if(touch.phase == UITouchPhaseMoved)
    {
        NSLog(@"UITouchPhaseMoved so returning no");
        return NO;
    }


    else
    {
        NSLog(@"returning super");
        NSLog(@"returning whatever this value is %d 0/1", [super touchesShouldBegin:touches withEvent:event inContentView:view]);
        [super touchesShouldBegin:touches withEvent:event inContentView:view];
        return [super touchesShouldBegin:touches withEvent:event inContentView:view];
    }
}

當我運行我的應用程序時,輕按按鈕時會調用上面的函數,但是當我在滾動視圖中的任意位置(按鈕的內部或外部)拖動時,不會調用該函數。 無論我試圖拖動到哪里,滾動視圖都不會滾動。

這是我與滾動視圖相關的所有代碼(由IB設置):

            smallView.scrollEnabled = YES;
            smallView.contentSize = CGSizeMake(smallView.frame.size.width, smallView.frame.size.height*1.4);
            smallView.delegate = self;

如果需要的話,以下是與按鈕相關的代碼:

NSArray *buttonsArray = @[button1, button2, button3, button4, button5];
for (int i = 0; i < [buttonsArray count]; i++) {
    UIButton *button = buttonsArray[i];
    button.tag = i+1;
    [button addTarget:self action:@selector(p_motivationButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
}

為什么我的滾動視圖不滾動甚至不調用touchesShouldBegin

子類化UIScrollView並重寫touchesShouldCancelInContentView() ,如下所示:

override func touchesShouldCancelInContentView(view: UIView) -> Bool {
    if let _ = view as? UIControl {
        return true
    }
    else {
        return super.touchesShouldCancelInContentView(view)
    }
}

暫無
暫無

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

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