suM*_*uMi 52 objective-c uigesturerecognizer ios uitapgesturerecognizer
我是手势识别器的新手所以也许这个问题听起来很愚蠢:我正在为一堆UIViews分配轻拍手势识别器.在该方法中,有可能找出以某种方式点击它们中的哪一个,或者我是否需要使用点击屏幕上的点找到它?
for (NSUInteger i=0; i<42; i++) {
float xMultiplier=(i)%6;
float yMultiplier= (i)/6;
float xPos=xMultiplier*imageWidth;
float yPos=1+UA_TOP_WHITE+UA_TOP_BAR_HEIGHT+yMultiplier*imageHeight;
UIView *greyRect=[[UIView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
[greyRect setBackgroundColor:UA_NAV_CTRL_COLOR];
greyRect.layer.borderColor=[UA_NAV_BAR_COLOR CGColor];
greyRect.layer.borderWidth=1.0f;
greyRect.userInteractionEnabled=YES;
[greyGridArray addObject:greyRect];
[self.view addSubview:greyRect];
NSLog(@"greyGrid: %i: %@", i, greyRect);
//make them touchable
UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter)];
letterTapRecognizer.numberOfTapsRequired = 1;
[greyRect addGestureRecognizer:letterTapRecognizer];
}
Run Code Online (Sandbox Code Playgroud)
Man*_*ani 111
highlightLetter:
使用参数定义目标选择器()
UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightLetter:)];
Run Code Online (Sandbox Code Playgroud)
然后你就可以看到了
- (void)highlightLetter:(UITapGestureRecognizer*)sender {
UIView *view = sender.view;
NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped.
}
Run Code Online (Sandbox Code Playgroud)
Ift*_*ari 15
一年来一直在问这个问题,但仍然是某人.
声明UITapGestureRecognizer
特定视图时将标记指定为
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandlerMethod:)];
[yourGestureEnableView addGestureRecognizer:tapRecognizer];
yourGestureEnableView.tag=2;
Run Code Online (Sandbox Code Playgroud)
并在您的处理程序中这样做
-(void)gestureHandlerMethod:(UITapGestureRecognizer*)sender {
if(sender.view.tag == 2) {
// do something here
}
}
Run Code Online (Sandbox Code Playgroud)
这是Swift 3的更新,也是Mani答案的补充.我建议sender.view
结合使用标记UIViews(或其他元素,取决于你想要跟踪的内容)来获得更加"先进"的方法.
let yourTapEvent = UITapGestureRecognizer(target: self, action: #selector(yourController.yourFunction))
yourObject.addGestureRecognizer(yourTapEvent) // adding the gesture to your object
Run Code Online (Sandbox Code Playgroud)
在同一个testController中定义函数(这是View Controller的名称).我们将在这里使用标签 - 标签是Int ID,您可以将其添加到UIView中yourButton.tag = 1
.如果你有一个动态的元素列表,比如一个数组你可以做一个for循环,它遍历你的数组并添加一个标签,它会逐渐增加
func yourFunction(_ sender: AnyObject) {
let yourTag = sender.view!.tag // this is the tag of your gesture's object
// do whatever you want from here :) e.g. if you have an array of buttons instead of just 1:
for button in buttonsArray {
if(button.tag == yourTag) {
// do something with your button
}
}
}
Run Code Online (Sandbox Code Playgroud)所有这一切的原因是因为当与#selector一起使用时,你不能为你的函数传递更多的参数.
如果您有一个更复杂的UI结构,并且您希望获得附加到您的点击手势的项目的父标记,您可以使用let yourAdvancedTag = sender.view!.superview?.tag
例如在UIView内获取按下按钮的UIView标记; 可用于缩略图+按钮列表等.
在 Swift 中使用此代码
func tappGeastureAction(sender: AnyObject) {
if let tap = sender as? UITapGestureRecognizer {
let point = tap.locationInView(locatedView)
if filterView.pointInside(point, withEvent: nil) == true {
// write your stuff here
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
很快就很简单
在 ViewDidLoad() 函数中编写此代码
let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler(gesture:)))
tap.numberOfTapsRequired = 2
tapView.addGestureRecognizer(tap)
Run Code Online (Sandbox Code Playgroud)
Handler 部分这可以在viewDidLoad 或viewDidLoad 之外,batter 放在扩展中
@objc func tapHandler(gesture: UITapGestureRecognizer) {
currentGestureStates.text = "Double Tap"
}
Run Code Online (Sandbox Code Playgroud)
在这里,我只是通过打印输出来测试代码,如果你想做一个动作,你可以做任何你想做的事情或者更多的练习和阅读
归档时间: |
|
查看次数: |
107546 次 |
最近记录: |