我添加手势以查看以下步骤:

@interface iCarousel () <UIGestureRecognizerDelegate>
{
     UIPanGestureRecognizer * mPanGesture;
     UITapGestureRecognizer * mTapGesture;
}
@end
@implementation iCarousel
- (void)setUp
{
    _contentView = [[UIView alloc] initWithFrame:self.bounds];

    _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    /
    mPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(iCarouselDidPan:)];
    mPanGesture.delegate = self;
    [_contentView addGestureRecognizer:mPanGesture];

    /
    mTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iCarouselDidTap:)];
    mTapGesture.delegate = self;
    [_contentView addGestureRecognizer:mTapGesture];

    /
    self.accessibilityTraits = UIAccessibilityTraitAllowsDirectInteraction;
    self.isAccessibilityElement = YES;

    [self addSubview:_contentView];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self setUp];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        [self setUp];
    }
    return self;
}

...


- (void)iCarouselDidTap:(UITapGestureRecognizer *)tapGesture
{
     ...
}


- (void)iCarouselDidPan:(UIPanGestureRecognizer *)panGesture
{
     ...
}

...

@end

现在,该类中的方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gesture

还给我

但是,不会触发iCarouselDidPan:和iCarouselDidTap:动作。

信息以gestureRecognizerShouldBegin:方法打印。
(lldb) po _contentView.gestureRecognizers
<__NSArrayI 0x1572bec0>(
<UIPanGestureRecognizer: 0x16dea750; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidPan:, target=<iCarousel 0x16dea1b0>)>>,
<UITapGestureRecognizer: 0x16e8f310; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidTap:, target=<iCarousel 0x16dea1b0>)>>
)
(lldb) po gesture
<UIPanGestureRecognizer: 0x16dea750; state = Possible; view = <UIView 0x16dea640>; target= <(action=iCarouselDidPan:, target=<iCarousel 0x16dea1b0>)>>

目前仅在设备上发生。

最佳答案

尝试在UIGestureRecognizerDelegate中实现以下协议iCarousel的方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceiveTouch:(UITouch *)touch {

    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceivePress:(UIPress *)press {

    return YES;
}

07-27 19:04