NSArray *increaseColorButtonArray = @[self.increaseRedButton, self.increaseGreenButton, self.increaseBlueButton, self.increaseWhiteButton];
UILongPressGestureRecognizer *increaseLongPressGesRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(increaseColorRecognizer:)];
;
for (UIButton *item in increaseColorButtonArray) {
[item addGestureRecognizer:increaseLongPressGesRec];
}
请看上面一段代码有什么问题,通过for循环给一个数组中的Button添加长按手势,可是长按手势却不起作用。
原因:一个手势对象只能够添加到一个对象上面,像上面的这种方式只能使最后一个被添加手势的对象起作用。
正确的添加方法如下:
NSArray *increaseColorButtonArray = @[self.increaseRedButton, self.increaseGreenButton, self.increaseBlueButton, self.increaseWhiteButton];
UILongPressGestureRecognizer *increaseLongPressGesRec;
for (UIButton *item in increaseColorButtonArray) {
increaseLongPressGesRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(increaseColorRecognizer:)];
[item addGestureRecognizer:increaseLongPressGesRec];
}