0

아래와 같이 UICollectionView를 만듭니다. UICollectionViewCell에있는 UIButton에 대한 UIEvent를 차단하고 싶습니다.

enter image description here

내가이 UICollectionView에 대해하고 싶은 것은

,

  1. 각 UICollectionViewCell가있는 UIButton있다 ([cell.contentView addSubview : 버튼])
  2. 있는 UIButton이 UIControlEventTouchDown 및 UIControlEventTouchUpInside을 처리해야
  3. UICollectionView 셀을 길게 눌러 대화 형 작업 시작
  4. 시작하지 마세요. UIButton을 길게 눌러 대화 형 작업. UIButton은 2 단계에서 할당 한 UIControlEvent를 처리합니다.
  5. UIButton 이외의 UICollectionViewCell 영역을 두드리면 (didSelectItemAtIndexPath :) 처리하고 싶습니다.

내 문제는 UICollectionView에 대한 handleLongPress가 응답과 긴있는 UIButton 도청 경우에도 interactiveMovement을 시작한다는 것입니다.

UIButton 이외의 UICollectionView 영역을 두드리는 경우에만 interactiveMovement를 시작하고 싶습니다.

UIButton을 길게 누르면 UIConEvent가 UIButton에 할당되고 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 
} 
+0

코드가 모든 문제를 해결 –

+0

@Sh_Khan 번호를 때 나는 길게 누르면있는 UIButton UICollectionView의 InteractiveMovement를 시작합니다. UICuttonView가 아니라 UIControlViewCell을 길게 누르면 시작할 수 있습니다. 고맙습니다. –

답변

0

투명 에 UIView를 추가있는 UIButton아래의 전체 컬렉션을보기에 긴 눌러를하지 추가 UIButton이 고려됨에 따라 셀의 하위 제어

또는

는 만약 Y 긴 언론 점 x가, 내 문제를 해결 한 방법

+0

의견을 보내 주셔서 감사합니다. 그러나 가능한 경우 UICollectionView에서 현재 길게 누르는 제스처를 변경하고 싶지 않습니다. UIButton에 대한 모든 터치 이벤트를 차단하는 다른 방법이 있습니까? –

+0

업데이트보기 ....... –

+0

시도해 보니 ** handleLongPress ** 메소드에서 반환 될 수 있습니다. 하지만이 경우 UIButton을 터치하면 ** UIControlEventTouchUpInside **가 호출되지 않습니다. –

0

에서 버튼의 프레임 리턴으로 교차 참조하십시오. 좋은 해결책은 아니지만 기대했던대로 작동합니다.

ButtonBaseView (: UIView)를 UIButton 아래에 추가하고 ButtonBaseView에 LongPressGesture를 추가했습니다.

LongPressGesture는 아무 동작도하지 않습니다. 역할은 LongPressGesture가 UICollectionView에 이벤트를 전달하지 않도록 차단하는 것입니다. 는 동시에 처리 할 cancelsTouchesInView=NO을 설정 TouchUpInside 또는 TouchDragExit

enter image description here

내 코드는 다음과 같다

- (void)addDummyLongTapGesture 
{ 
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil]; 
    gesture.cancelsTouchesInView = NO; 
    [buttonBaseView addGestureRecognizer:gesture]; 
} 
관련 문제