移除手势识别器的delaysTouchesBegan属性。
示例代码:
Swift:
yourButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
let tapGesture = UIPanGestureRecognizer(target: self, action: nil)
tapGesture.delaysTouchesBegan = false
yourButton.addGestureRecognizer(tapGesture)
@objc func buttonAction(_ sender: UIButton) {
print("Button tapped")
}
Objective-C:
[yourButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:nil];
panGesture.delaysTouchesBegan = NO;
[yourButton addGestureRecognizer:panGesture];
- (void)buttonAction:(UIButton *)sender {
NSLog(@"Button tapped");
}
下一篇:按钮在框架中不显示。