Pinch & Rotate: UIGestureRecognizerDelegate not called

痞子三分冷 提交于 2019-12-23 13:05:18

问题


I'm trying to implement both a pinch and a rotate in the same view. Sometimes the pinch selector gets called and sometimes the rotation one, ok, but then it sticks with it. The problem is clearly that gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer isn't being called. Why not? Got to be something obvious...

@interface FaceView : UIView <UIGestureRecognizerDelegate>
{
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end

@implementation FaceView
- (id)initWithFrame:(CGRect)frame
{
    if( self = [super initWithFrame:frame] )
    {
        self.multipleTouchEnabled = YES;
        self.userInteractionEnabled = YES;

        UIRotationGestureRecognizer* rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
        [self addGestureRecognizer:rotationRecognizer];
        [rotationRecognizer release];

        UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
        [self addGestureRecognizer:pinchRecognizer];
        [pinchRecognizer release];
    }
    return self;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer");
    return YES;
}

- (void)rotationGesture:(UIRotationGestureRecognizer*)gesture
{
    switch( gesture.state )
    {
        case UIGestureRecognizerStateBegan:
            NSLog(@"rotationGesture began");
            break;

        case UIGestureRecognizerStateChanged:
            NSLog(@"rotationGesture changed");
            break;
    }
}

- (void)pinchGesture:(UIPinchGestureRecognizer*)gesture
{
    switch( gesture.state )
    {
        case UIGestureRecognizerStateBegan:
            NSLog(@"pinchGesture began");
            break;

        case UIGestureRecognizerStateChanged:
            NSLog(@"pinchGesture changed");
            break;
    }
}
....

回答1:


I had to set rotationRecognizer.delegate = self; and pinchRecognizer.delegate = self;



来源:https://stackoverflow.com/questions/7801721/pinch-rotate-uigesturerecognizerdelegate-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!