1

UIScrollViewをサブクラス化し、touchesShouldCancelInContentView:メソッドを次のようにオーバーライドしました。

-(BOOL)touchesShouldCancelInContentView:(UIView *)view

{
    if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UISegmentedControl class]]) {
        return YES;
    }

    if ([view isKindOfClass:[UIControl class]]) {
        return NO;
    }

    return YES;
}

UIButtonには完全に機能しますが、UISegmentedControlには機能しません。どんな助けでもいただければ幸いです

4

1 に答える 1

1

ついにそれを理解した。UISegmentedControlとUIScrollViewの両方をサブクラス化する必要がありました。

1.)UISegmentedControlの新しいプロパティを追加しました:

@property (nonatomic, assign) BOOL touchProcessed;

2.)UISegmentedControlの次のメソッドをオーバーロードしました。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (touchProcessed) {
        [super touchesBegan:touches withEvent:event];
    }
    else {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    self.touchProcessed = NO;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesEnded:touches withEvent:event];
}

3.)UIScrollviewの次のメソッドをオーバーロードしました。

-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{ 
    if ([view isKindOfClass:[self class]])
    {
        return YES; //if there are two nested custom scrollviews  
    }

    if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[CustomSegmentedControl class]] || [[view superview] isKindOfClass:[CustomSegmentedControl class]]) {
        return YES;
    }

    if ([view isKindOfClass:[UIControl class]]) {
        return NO;
    }

    return YES;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging) {
        for (UIView *sv in self.subviews) {
            if ([sv isKindOfClass:[CustomSegmentedControl class]])
            {   //if there is more than one add a tag or tomething
                [(CustomSegmentedControl *)sv setTouchProcessed:YES];
                [sv touchesBegan:touches withEvent:event];
                return;
             }
        }
    }
}

完璧に動作します!

于 2013-01-18T17:53:00.367 に答える