4

UIScrollView 内に UIView があり、UIView 内にはボタンがあります。問題は、そのボタンを押したままにして (この場合はボタンの状態が押されている)、スクロールしようとすると、スクロール ビューがスクロールしないことです。それはどこにあるべきか。UIView にはジェスチャ認識機能があり、そのデリゲートの 1 つを使用して、UIButton を押してスクロールした場合にスクロール ビューをスクロールできるようにしようとしています。どうすればいいですか?

基本的に要約すると、ボタンが押された/保持された場合、タッチイベントをスクロールビューに渡す必要があります。ボタンからのタッチアップ イベントである場合は、明らかにボタンのアクションをトリガーし、スクロールしないようにする必要があります。

4

4 に答える 4

5

Old question, but I just had this issue and thought people would benefit from the answer. If you have a UIControl inside a UIScrollView, by default the scroll won't cancel the touch event. The solution is to subclass the UIScrollView like this:

@implementation PaginationScrollView {}

- (id)init {
    self = [super init];
    if (self) {
        self.canCancelContentTouches = YES;
    }
    return self;
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return YES;
}

@end

the default implementation of touchesShouldCancelInContentView returns NO if the view is a UIControl.

于 2012-08-23T20:58:15.450 に答える
3

設定を確認

yourScrollView.canCancelContentTouches = YES;

まだ動作していません?UIControlEventTouchUpInsideなどのUIControlEventsではなく、タッチのみをキャンセルするため

の解き方?.mこれをファイルの先頭に追加します

@implementation UIScrollView (TouchesShouldCancelInContentView)

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return YES;
}

@end
于 2013-11-21T16:01:33.217 に答える
0

UIButtonの単純なTouchUpInsideアクションにUIGestureRecognizerを追加する必要はありません。単に、次のようにします。

[button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];

次に、セレクターを作成します。

-(IBAction)buttonSelect:(id)sender{//do stuff here}
于 2012-07-16T15:34:21.403 に答える
0

UIButtonを相互作用しないようにしてみてください。

button.userInteractionEnabled = NO;

次に、UITapGestureRecognizerをボタンに追加します。

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed)];
[button addGestureRecognizer:recognizer];

そうすれば、ボタンがタップされた場合にのみタッチイベントに反応し、他のすべてのイベントはスクロールビューに移動します。

userInteractionEnabled = NOに設定すると、UITapGestureRecognizerがそのイベントを起動できなくなる可能性があります。その場合、ボタンをUIViewまたはUIImageViewにすることができます。

于 2012-07-16T17:49:23.613 に答える