我正在创建如下的UICollectionView。
我想要做的关于UICollectionView的是,
我的问题是,即使长时间点击UIButton,handleLongPress for UICollectionView也会响应并启动interactiveMovement。
我想只在轻触UIBollection以外的UICollectionView区域时启动interactiveMovement。
当长按UIButton时,我只想处理分配给UIButton的UIConrolEvent,并且不希望UICollectionView响应。
如果你有一些解决方案我会很感激。
我的代码如下,谢谢。
MyCollectionView:UICollectionView
- (id)init
{
self = [super initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
if (self) {
self.delegate = self;
self.dataSource = self;
--- snip ---
[self addLongPressGesture];
--- snip ---
}
return self;
}
- (void)addLongPressGesture
{
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];
[self addGestureRecognizer:gesture];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self indexPathForItemAtPoint:point];
switch (sender.state) {
case UIGestureRecognizerStateBegan:
[self beginInteractiveMovementForItemAtIndexPath:indexPath];
break;
case UIGestureRecognizerStateChanged:
[self updateInteractiveMovementTargetPosition:point];
break;
case UIGestureRecognizerStateEnded:
[self endInteractiveMovement];
break;
default:
[self cancelInteractiveMovement];
break;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// something to do
}
MyUICollectionViewCell:UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initButton];
}
return self;
}
- (void)initButton
{
_button = [[UIButton alloc] init];
[_button addTarget:self action:@selector(touchUpButton:) forControlEvents:UIControlEventTouchUpInside];
[_button addTarget:self action:@selector(touchDownButton:) forControlEvents:UIControlEventTouchDown];
--- snip ---
[self addSubview:_button];
}
- (void)touchUpButton:(UIButton *)button
{
// something to do
}
- (void)touchDownButton:(UIButton *)button
{
// something to do
}
在UIButton下面添加一个透明的UIView并向其添加长按不到整个集合视图,因为UIButton被认为是单元格的子控件
要么
看到长按点x,y,如果它与方法的按钮框架返回相交
我已经解决了我的问题。我不是一个好的解决方案,但它正如我所期望的那样工作。
我在UIButton下添加了ButtonBaseView(:UIView),并在ButtonBaseView上添加了LongPressGesture。
LongPressGesture没有采取任何行动。该角色是阻止LongPressGesture不将事件传递给UICollectionView。同时,设置cancelsTouchesInView=NO
来处理TouchUpInside
或TouchDragExit
我的代码如下,
- (void)addDummyLongTapGesture
{
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
gesture.cancelsTouchesInView = NO;
[buttonBaseView addGestureRecognizer:gesture];
}