我希望能够将这些线条切断:
[button0 addGestureRecognizer:longPress];
[button1 addGestureRecognizer:longPress];
[button2 addGestureRecognizer:longPress];
[button3 addGestureRecognizer:longPress];
[button4 addGestureRecognizer:longPress];
[button5 addGestureRecognizer:longPress];
[button6 addGestureRecognizer:longPress];
[button7 addGestureRecognizer:longPress];
[button8 addGestureRecognizer:longPress];
[button9 addGestureRecognizer:longPress];
Run Code Online (Sandbox Code Playgroud)
等等.一路到36 !!
可能有一个循环?但我不知道该怎么做.
感谢和问候.
您可以为每个按钮分配标签,并使用该方法循环按钮viewWithTag.
for (int i = 0; i < 36; i++) {
UIButton *button = [self.view viewWithTag:i];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[button addGestureRecognizer:longPress];
}
Run Code Online (Sandbox Code Playgroud)
以下屏幕截图显示了在Interface Builder中为每个按钮分配标记的位置.

如果您为按钮设置了IBOutlets,则可以使用valueForKey:和不使用标记来获取它们:
for (int i = 0; i < 36; i++) {
NSString *key = [NSString stringWithFormat:@"button%d", i];
UIButton *button = [self valueForKey:key];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[button addGestureRecognizer:longPress];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2017 次 |
| 最近记录: |