gpt4 book ai didi

iphone - 如何检测 subview 中的触摸事件或当我们触摸 subview 时如何使父 View 被触摸?

转载 作者:行者123 更新时间:2023-12-03 18:51:32 24 4
gpt4 key购买 nike

我有 2 个 UIView。

第一个是父 View

第二个是 subview ,

我们如何检测 subview 何时被触摸?

或者我想在用户触摸 subview 时使父 View 被触摸,任何代码可以帮助我做到这一点吗?可以这样做吗?

因为我有一个 Something Function,当其中一个函数被触摸时它就会调用。

最佳答案

这对我有用:

(链接xib或storyboard中的 subview )

ViewController.h

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIView *subview;
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;

@end

ViewController.m

@implementation ViewController

@synthesize subview;
@synthesize tapRecognizer;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];

[subview addGestureRecognizer:tapRecognizer];
}

- (IBAction)handleTap:(UITapGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateEnded){
//code here
NSLog(@"subview touched");
}
}

@end

关于iphone - 如何检测 subview 中的触摸事件或当我们触摸 subview 时如何使父 View 被触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907669/

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