gpt4 book ai didi

iphone - cocoa-touch:如何将触摸传递给另一个对象

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:56 24 4
gpt4 key购买 nike

恐怕这是一个初学者的问题:

我有一个覆盖整个屏幕的 UIText。我在此 UITextView 之上有另一个透明 View ,以便能够识别滑动手势(水平和垂直),如下所示:

    - (void)viewDidLoad
{
[super viewDidLoad];

// UITextView
CGRect aFrame = CGRectMake(0, 0, 320, 480);
aTextView = [[UITextView alloc] initWithFrame:aFrame];
aTextView.text = @"Some sample text.";
[self.view addSubview:aTextView];

// canTouchMe
CGRect canTouchMeFrame = CGRectMake(0, 0, 320, 480);
canTouchMe = [[UIView alloc] initWithFrame:canTouchMeFrame];
[self.view addSubview:canTouchMe];
}

让我们考虑用户触摸(而不是滑动)canTouchMe View 。在这种情况下,我希望 canTouchMe View 消失并将触摸传递给隐藏在下方的 UITextView,以便它进入编辑模式并启用 UITextView 具有的“自然”滚动选项(即仅水平滚动)。

我的触摸开始方法如下所示:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];

UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}

如果它只识别ONE touch,我如何告诉这个方法它应该隐藏 canTouchMeFrame 并将PASS ON the touch 给 UITextView?

对不起,如果这是基本的,但我不知道如何实现它。感谢您的任何建议。


编辑:

我引入了一个touchEnded方法,但是还是没有运气。触摸不会转发到 UITextView。我需要点击两次才能对其进行编辑:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

[super touchesMoved:touches withEvent:event];

UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];

CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive


if (deltaY == 0 && deltaX == 0) {

label.text = @"Touch"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

[aTextView touchesBegan:touches withEvent:event];

[self.view bringSubviewToFront:aTextView];
[self.view bringSubviewToFront:doneEdit];


}

最佳答案

NSSet 有一个 -count 方法。如果 touches 只有一个对象,那么您就是在响应单次触摸。

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];
if ([touches count] == 1) {
[self hideMyRectangle];
[someOtherObject touchesBegan:touches withEvent:event];
//etc, etc.
return;
}
// if you get here, there's more than one touch.
UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}

关于iphone - cocoa-touch:如何将触摸传递给另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com