所以我有一个方法,然后在调用时会生成一个简单的 UIView,其中包含一些标签:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 250)];
view1.backgroundColor = [UIColor redColor];
view1.userInteractionEnabled = YES;
[self.view addSubview:view1];
我调用了这个方法 6 次,所以它在屏幕周围放置了 6 个 UIView(当然我给了它们不同的坐标)。
如何检测用户何时在其中一个上向右滑动然后触发其他方法?
我试过这样的事情:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myLabel1Tap)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[view1 addGestureRecognizer:swipeRight];
然后是一个方法:
- (void)myLabel1Tap {
}
但是我不确定在该方法中该怎么做,如果它们都被称为相同的“view1”,我怎么知道哪个 View 被刷了?
最佳答案
更改手势识别器选择器以接受参数(通过在方法签名后添加冒号)
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myLabel1Swipe:)];
这意味着手势识别器将被传入,然后您可以根据手势识别器属性执行操作,例如
- (void)myLabel1Swipe:(UISwipeGestureRecognizer *)recogniser
{
UIView *swipedView = [recognizer view];
//do whatever you want with this view
}
关于ios - 将事件添加到以编程方式生成的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892145/