UIViewControllerにラップされたフローティングボタンを作成しようとしています。 フローティングボタンのように:https://material.google.com/components/buttons-floating-action-button.html#userInteractionEnabled = NO; 1つのサブボタンを除いて
しかし、ボタンだけがタッチに反応するようにすることはできますか?
- (void)viewDidLoad {
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[closeButton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[closeButton setTitle:@"Back"
forState:UIControlStateNormal];
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0);
[self.view addSubview:closeButton];
self.view.userInteractionEnabled = NO;
closeButton.userInteractionEnabled = YES;
}
更新:私はのUIViewのサブクラスを追加はMYVIEWと呼ばれ、追加:
self.view = [[MyView alloc] init];
のNSLogショー:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitTestView = [super hitTest:point withEvent:event];
if (hitTestView == self) {
hitTestView = nil;
}
NSLog(@"%@", hitTestView);
return hitTestView;
}
とのViewControllerのviewDidLoadでは、最初の行としてこれを追加しましたいつも(ヌル)
「フローティング」ボタンとは何ですか? – matt
@matt OPは、closeButton以外のビュー全体を「無効にしたい」と思って、そのcloseButtonをユーザーが触れることのできない「背景」ビューの上に「フローティング」のように感じさせると思います。 – kennytm
'pointInside:withEvent:'をオーバーライドすると、ポイントがボタン内にある場合にのみtrueを返すことができます。ボタンのスーパービューで 'userInteractionEnabled'をnoに設定すると、ボタンがタッチイベントを受け取らなくなります。 – dan