UIButton
后面有一个UIScrollview
,我希望能够触摸该按钮。目前是否显而易见我只能对UIScrollview
执行操作,但我也希望能够对UIButton
答案 0 :(得分:1)
如果您希望UIScrollView
保持对用户的触摸,您可以尝试类似于Hemang suggests的内容。
我建议直接通过UIButton
的{{1}}事件发送信号。例如,在touchUpInside
:
UIViewController
我不知道你为什么曾想要这样做呢!
我现在知道你想要这样做,因为你希望滚动视图中的图像像按钮一样。我建议您只使用@implementation ViewController {
UIScrollView* scrollView;
UITapGestureRecognizer* tapGesture;
UIButton* button;
}
- (void)viewDidLoad {
[super viewDidLoad];
button = [[UIButton alloc] initWithFrame:(CGRect){30, 30, 300, 300}];
[button addTarget:self action:@selector(buttonPress) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){20, 20, 300, 500}];
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewWasTouched)];
[scrollView addGestureRecognizer:tapGesture];
[self.view addSubview:scrollView];
}
-(void) buttonPress {
NSLog(@"Button press!");
}
-(void) scrollViewWasTouched {
if (CGRectContainsPoint(button.frame, [tapGesture locationOfTouch:0 inView:self.view])) {
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
@end
个对象替换UIImageView
个对象。
然后,您可以直接将图片添加到UIButton
:
UIButton
答案 1 :(得分:0)
您可以覆盖方法
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
在scrollView和Button的superview中。此方法应返回特定点的接收器。所以在你的情况下它应该是那样的
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if CGRectContainsPoint(button.frame, point) {
return button
}
return super.hitTest(post, withEvent: event)
}