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

发布于 2024-11-27 12:01:04 字数 4188 浏览 0 评论 0原文

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

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

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

导航栏中的共享框下拉菜单 http://dl.dropbox.com/u/1734050/navBarWithShareBox.jpg< /a>

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

问题是:调出共享框后我无法关闭导航栏!

这是一些代码:

-(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 方法,但考虑到它不起作用,我错了!我哪里做错了?如果您需要更多信息,我很乐意提供更多信息。

I have this lovely nav bar asset:

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

and when users tap on the middle button (one with the arrows) it will bring up this other lovely "share box" asset

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

What I have in the code so far is that when the screen is tapped, the nav bar will appear, and disappear on another tap. Secondly, when the user taps on the "share" button, the share box comes up, and the user can tap outside of the box to dismiss it.

Here is the problem: I cannot dismiss the nav bar after bringing up the share box!

Here is some code:

-(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;
    }   

}

Ok, so that should look all dandy (and works like it too!) -- now here is perhaps where it gets unorthodox?

-(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;
    }
}

I then create this method to handle the specific shareBox tapping:

-(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:)];
    }

}

I'm assuming my problem is coming from alloc/initing the new UITapGestureRecognizer in the -(IBAction)showShareMenu method. I thought by using the removeTarget... and addTarget messages I could easily tell my code which TapGesture method it should use, but considering it isn't working, I was wrong! Where did I go wrong? If you need more info, I'd be happy to give more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

节枝 2024-12-04 12:01:04

啊哈!看来我找到了解决办法!在我的 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 没有达到目的。

Ah Ha! Looks like I found a solution! In my handleTapForShareBox method I did this:

-(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];
    }
}

It works great now! However, there were a couple things:

1) Do I have to release the UITapGestureRecognizer local pointer I created? Or because I initWithTarget it will be autoreleased?

2) Is this a hack? Or is this a proper way of passing different tap methods around? I'm just a little confused why the removeTarget and addTarget didn't do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文