试图“打开/关闭”不同的UITapGestureReconginzer方法

时间:2011-07-30 19:21:31

标签: objective-c ios cocoa-touch uigesturerecognizer

我有这个可爱的导航栏资产:

navBar view http://dl.dropbox.com/u/1734050/ViewWithNavBar.jpg

当用户点击中间按钮(带有箭头的按钮)时,它将显示另一个可爱的“共享框”资产

shareBox dropdown from navBar http://dl.dropbox.com/u/1734050/navBarWithShareBox.jpg

到目前为止我在代码中的内容是,当点击屏幕时,导航栏会出现,并在另一个点击时消失。其次,当用户点击“共享”按钮时,共享框出现,用户可以在框外点击以解除它。

问题是:在打开共享框后我无法关闭导航栏!

以下是一些代码:

-(void)viewDidLoad {

...
...
...

    UITapGestureRecognizer *tapNavBar = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapForNavBar:)];
    self.tapForNavBar = tapNavBar;

    [self.viewForTaps addGestureRecognizer:self.tapForNavBar];

    // btw, tapForNavBar is a UITapGestureRecognizer instance variable.
    // also, viewForTaps is a view (instance variable) I made to handle 
    // where the user could tap (most of the screen)

}


#pragma mark -
#pragma mark Tap and Gesture Methods

-(void)handleTapForNavBar:(UIGestureRecognizer *)gestureRecognizer {

    if (self.navBarIsHidden) {

        [UIView animateWithDuration:0.5 animations:^ {
            //self.primeViewController.view.alpha = 0.8;
            self.navBar.alpha = 1.0;
        }];
        self.detailViewButton.userInteractionEnabled = YES;
        self.shareButton.userInteractionEnabled = YES;
        self.aboutAppButton.userInteractionEnabled = YES;
        self.navBarIsHidden = NO;
    }
    else {
        [UIView animateWithDuration:0.5 animations:^ {
            self.navBar.alpha = 0.0;
            //self.primeViewController.view.alpha = 1.0;
        }];
        self.detailViewButton.userInteractionEnabled = NO;
        self.shareButton.userInteractionEnabled = NO;
        self.aboutAppButton.userInteractionEnabled = NO;
        self.navBarIsHidden = YES;
    }   

}

好的,所以看起来应该是所有的花花公子(并且也像它一样工作!) - 现在这里可能是非正统的地方?

-(IBAction)showShareMenu:(id)sender {

    if (self.navBarShareBoxIsHidden) {
        [UIView animateWithDuration:0.5 animations:^ {
            self.navBarShareBox.alpha = 1.0;
        }];  
        [self.tapForNavBar removeTarget:nil action:NULL];
        UITapGestureRecognizer *tapShareBox = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapForShareBox:)];
        self.tapForShareBox = tapShareBox;

        [self.viewForTaps addGestureRecognizer:self.tapForShareBox];
        self.navBarShareBoxIsHidden = NO;
        self.twitterButton.userInteractionEnabled = YES;
        self.facebookButton.userInteractionEnabled = YES;
        self.googlePlusButton.userInteractionEnabled = YES;
    }
    else {
        [UIView animateWithDuration:0.5 animations:^ {
            self.navBarShareBox.alpha = 0.0;
        }];  
        [self.tapForShareBox removeTarget:nil action:NULL];
        [self.tapForNavBar addTarget:self action:@selector(handleTapForNavBar:)];
        self.navBarShareBoxIsHidden = YES;
        self.twitterButton.userInteractionEnabled = NO;
        self.facebookButton.userInteractionEnabled = NO;
        self.googlePlusButton.userInteractionEnabled = NO;
    }
}

然后我创建此方法来处理特定的shareBox攻击:

-(void)handleTapForShareBox:(UIGestureRecognizer *)gestureRecognizer {

    if (!self.navBarShareBoxIsHidden) {
        [UIView animateWithDuration:0.5 animations:^ {
            self.navBarShareBox.alpha = 0.0;
        }];
        self.navBarShareBoxIsHidden = YES;
        self.twitterButton.userInteractionEnabled = NO;
        self.facebookButton.userInteractionEnabled = NO;
        self.googlePlusButton.userInteractionEnabled = NO;
        [self.tapForShareBox removeTarget:nil action:NULL];
        [self.tapForNavBar addTarget:self action:@selector(handleTapForNavBar:)];
    }

}

我假设我的问题来自alloc / init UITapGestureRecognizer方法中的新-(IBAction)showShareMenu。我想通过使用removeTarget ...和addTarget消息,我可以很容易地告诉我的代码应该使用哪种TapGesture方法,但考虑到它不起作用,我错了!我哪里做错了?如果您需要更多信息,我很乐意提供更多信息。

1 个答案:

答案 0 :(得分:0)

啊哈!看起来我找到了解决方案!在我的handleTapForShareBox方法中,我这样做了:

-(void)handleTapForShareBox:(UIGestureRecognizer *)gestureRecognizer {

    if (!self.navBarShareBoxIsHidden) {
        [UIView animateWithDuration:0.5 animations:^ {
            self.navBarShareBox.alpha = 0.0;
        }];
        self.navBarShareBoxIsHidden = YES;
        self.twitterButton.userInteractionEnabled = NO;
        self.facebookButton.userInteractionEnabled = NO;
        self.googlePlusButton.userInteractionEnabled = NO;
        [self.tapForShareBox removeTarget:nil action:NULL];
        //[self.tapForNavBar addTarget:self action:@selector(handleTapForNavBar:)];

        // Here is the new stuff, I added another alloc init of a UITapGestureRecognizer
        // and re-assigned it to my UITap instance variable

        UITapGestureRecognizer *tapNavBar = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapForNavBar:)];
        self.tapForNavBar = tapNavBar;

        [self.viewForTaps addGestureRecognizer:self.tapForNavBar];
    }
}

现在效果很好!但是,有几件事情:

1)我是否必须释放我创建的UITapGestureRecognizer本地指针?或者因为我initWithTarget它会被自动释放?

2)这是一个黑客吗?或者这是通过不同的点击方法的正确方法?我只是有点困惑为什么removeTargetaddTarget没有做到这一点。