您的位置:首页 > 产品设计 > UI/UE

[IOS]UILongPressGestureRecognizer 执行两次的问题

2014-05-22 09:22 495 查看
[align=center]UILongPressGestureRecognizer 执行两次的问题

[/align]
    IOS长按手势,默认长按后会两次进入所关联的方法,若只需要改变一次状态就需要用代码将两次方法响应分隔开...

    长按Button右上弹出一个小叉,再次长按Button右上小叉消失

//每个UICollectionView展示的内容
-(BJNewsShelfCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BJNewsShelfCell *cell = (BJNewsShelfCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

//添加长按手势
UILongPressGestureRecognizer *pressGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPressAction:)];
[cell addGestureRecognizer:pressGes];
pressGes.minimumPressDuration = 1.0;//触发长按事件的时间是1秒
pressGes.view.tag = theRow;
return cell;
}

//添加长按手势
-(void)cellLongPressAction:(UILongPressGestureRecognizer *)sender{
//这里长按一次会进入两次
if (sender.state == UIGestureRecognizerStateEnded  || sender.state ==UIGestureRecognizerStateChanged) {
//sender.state ==UIGestureRecognizerStateChanged这个限制不添加也不影响执行一次的功能,只是若长按住View不放手,一直在上面拖动的话,会一直响应下面else
}else {
//这里长按一次只会进入一次
 if(_isLongPress == 1){
_isLongPress = 0;
}else{
_isLongPress = 1;
}
NSLog(@"cellLongPressAction");

[_collectionView reloadData];
}
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: