我们需要用两种方法设置一个UIButton
,第一种是当你触摸它(向下和向上)时你得到一个 Action ,但是当你长按它时,你得到另一个。
例如,当您长按时获取有关它的数据,而当常规点击时,则另当别论。
如何使用 UIButton 实现此目的?
UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
CAT.backgroundColor=[UIColor clearColor];
[CAT addTarget:self action:@selector(newcat:)forControlEvents:UIControlEventTouchUpInside];
我已经开始用这个给它添加一个手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[CAT addGestureRecognizer:longPress];
但这只有在您松开手指时才会触发。 我希望在 1-2 秒后我的手指还在那里时触发它。 我可以这样做吗?我可以调整它触发所需的时间吗?
最佳答案
您可以使用事件UIControlEventTouchDown
,为长按使用延迟时间并且您必须处理UIControlEventTouchUpInside
和UIControlEventTouchUpOutside
。祝你好运!
关于ios - UIButton 的两种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753743/