我想創建一個包裹在UIViewController中的浮動按鈕。 像這樣的浮動按鈕:https://material.google.com/components/buttons-floating-action-button.html#userInteractionEnabled = NO;除了一個子按鈕
但是我怎樣才能讓按鈕響應觸摸?
- (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的和補充:
- (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中加入此作爲第一行:
self.view = [[MyView alloc] init];
的NSLog的節目始終(null)
什麼是「浮動」按鈕? – matt
@matt我猜OP想禁用除closeButton之外的整個視圖,使得closeButton感覺像在用戶無法觸摸的「背景」視圖上方「浮動」。 – kennytm
你可以覆蓋'pointInside:withEvent:'只返回true,如果點在按鈕中。在按鈕的超級視圖中將'userInteractionEnabled'設置爲no將防止按鈕獲得觸摸事件。 – dan