gpt4 book ai didi

ios - 如何向 UIView 添加触摸事件?

转载 作者:IT老高 更新时间:2023-10-28 12:13:53 25 4
gpt4 key购买 nike

如何向 UIView 添加触摸事件?
我试试:

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease];
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown];
// ERROR MESSAGE: UIView may not respond to '-addTarget:action:forControlEvents:'

我不想创建子类并覆盖

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

最佳答案

在 iOS 3.2 及更高版本中,您可以使用手势识别器。例如,这是您处理点击事件的方式:

//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:[recognizer.view superview]];

//Do stuff here...
}

还有很多内置的手势。查看有关 iOS 事件处理和 UIGestureRecognizer 的文档。我在 github 上还有一堆示例代码这可能会有所帮助。

关于ios - 如何向 UIView 添加触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660371/

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