1

完全に制御できる独自の完全にカスタマイズされたボタンを作成するために、カスタム UIButton サブクラスを作成しています。それでも、その UIButton のビューにサブビューを追加しようとすると問題が発生します。サブビューは「タッチ」イベントをブロックするため、UIButton 自体にバブリングしません。

これはデモンストレーションです。まず、フレームを使用して CustomNavigationButton を作成します。マゼンタです。画面にマゼンタが見えるので、そこにあります。次に、その CustomNavigationButton (緑色) にサブビューを追加すると、そこに緑色が表示されますが、緑色の四角形 (サブビュー) をクリックすると、CustomNavigationButton で "UIControlEventTouchUpInside" が呼び出されません。

私のAppDeleteで:

CustomNavigationButton* mapBtn = [[CustomNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
mapBtn.backgroundColor = [UIColor magentaColor];
[mapBtn addTarget:self action:@selector(goMapHandler:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mapBtn];

そして、ここに私の CustomNavigationButton クラス (UIButton のサブクラス) があります。

@implementation CustomNavigationButton
@synthesize bg;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // STORE INITIAL FRAME
        self.initialFrame = frame;

        bg = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
        bg.backgroundColor = [UIColor greenColor];
        [bg setUserInteractionEnabled:YES];
        [self addSubview:bg];       

    }
    return self;
}
@end
4

1 に答える 1

4

やり方わかった!「bg」が UIButton に追加するサブビューである場合は、次のようにする必要があります。

bg.userInteractionEnabled = NO;
bg.exclusiveTouch = NO;

ただし、サブビューが UIButton のフレームを拡張すると、タッチが発生しないことに注意してください。UIButton に背景色を指定することで、サブビューが UIButton を超えているかどうかを確認できます。

于 2012-12-11T13:38:00.207 に答える