开发者

iOS view change with gesture problem

开发者 https://www.devze.com 2023-03-10 03:39 出处:网络
here is my problem: In mainviewcontroller I added to swipe down gesture to switch down another view, but insideother views added to ma开发者_运维知识库inviewcontroller with swipe down gesture opens th

here is my problem: In mainviewcontroller I added to swipe down gesture to switch down another view, but inside other views added to ma开发者_运维知识库inviewcontroller with swipe down gesture opens that specific view. However, I dont want swipe down gesture in other views rather than main view.

//---gesture recognizer

- (IBAction) handleSwipes:(UIGestureRecognizer *) sender {
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];

    if (direction == UISwipeGestureRecognizerDirectionDown) {
        shakeController = [[ShakeViewController alloc] 
                           initWithNibName:@"ShakeViewController" bundle:nil];

        CATransition *transition = [CATransition animation];
        transition.duration = 0.5;

        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        transition.type = kCATransitionMoveIn;
        transition.subtype = kCATransitionFromBottom;

        transition.delegate = self;

        // Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
        [self.view.layer addAnimation:transition forKey:nil];

        [self.view addSubview:shakeController.view];

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //gesture recognizer
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(handleSwipes:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeGesture];

    [swipeGesture release];
}

Thanks


You should adopt the UIGestureRecognizerDelegate protocol and implement gestureRecognizer:shouldReceiveTouch: method to signal whether the gesture recognizer should respond or not.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    for ( UIView *subview in self.view.subviews ) {
        CGPoint point = [touch locationInView:subview];
        UIView *hitView = [subview hitTest:point withEvent:nil];
        if ( hitView != nil ) {
            return NO; // Touching one of the subviews so we will not accept the gesture.
        }
    }
    return YES;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消