0

UItoolbaritems として 3 つのボタンを追加するツールバーがありました。現在、ツールバーにもジェスチャ認識機能を追加して、そのツールバーを表示および非表示にしています。私の機能を実行します。私はこれを試しました `

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -12, [[UIScreen mainScreen] bounds].size.width, 44);

    self.navigationController.toolbar.tintColor=[UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1.0];  


    buttonGoBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouchUp:)];

buttonGoBack.tag=1;

    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width = 30;


    buttonGoForward = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardButtonTouchUp:)];
    buttonGoForward.tag=2;

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.navigationController.toolbar addGestureRecognizer:gestureRecognizer];



      NSMutableArray *toolBarButtons = [[NSMutableArray alloc] init];

    [toolBarButtons addObject:buttonGoBack];
    [toolBarButtons addObject:fixedSpace];
    [toolBarButtons addObject:buttonGoForward];

`

そして、私はジェスチャ認識を次のように実行しました

 if(sender.view.tag==1)
           [self performSelector:@selector(backButtonTouchUp:) withObject:nil];
        else if(sender.view.tag==2)
           [self performSelector:@selector(forwardButtonTouchUp:) withObject:nil];
         else
          {
     [UIView animateWithDuration:.50 
                      animations:^{
         if(self.navigationController.toolbar.frame.origin.y==[[UIScreen mainScreen] bounds].size.height -12)
                         self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -44, [[UIScreen mainScreen] bounds].size.width, 44);
         else
                            self.navigationController.toolbar.frame=CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -12, [[UIScreen mainScreen] bounds].size.width, 44);   

                      }]
}

` しかし、ボタンのアクションが実行されません。どこが間違っているのか、誰か助けてくれませんか?

4

3 に答える 3

1

このソリューションはあなたを助けるかもしれません..! ジェスチャ アクションに次のようにisKindOfClassを指定できます。

たとえば、次のようにして実行できます:-

 NSArray *subs = [self.yourviewcontroller.view subviews];
    for (UIToolbar *child in subs) {
        if ([child isKindOfClass:[UIToolbar class]]) {

            for (UIView *v in [child items])
            {
                if ([v isKindOfClass:[UIBarButtonItem class]])
                {
                    UIBarButtonItem *b = (UIBarButtonItem*)v;

                    NSLog(@"found");
                    //do something with b you can also fond your clickable Barbutton Item.
                }
            }

        }
    }

または、提案されている他の解決策ですが、Herçulesデリゲートですが、これを以下のように編集する必要があります

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
            shouldReceiveTouch:(UITouch *)touch {

    if (touch.view == buttonGoForward) {
        return NO;
    }
    if (touch.view == buttonGoBack) {
        return NO;
    }

    return YES;
}

このリファレンスにアクセスしてください:-

Button Tap イベントは Tap Gesture Recognizer によってオーバーライドされますか?

于 2013-07-16T12:52:30.160 に答える
1

UIGesture Viewデリゲートにより、UIBarButtonItemがクリックされたときにタッチを無視でき、UIToolBarをクリックすると、コーディングしたようにツールバーが非表示になります:-

 gestureRecognizer.delegate=self;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (![touch.view isKindOfClass:[UIToolbar class]]) {
        return NO;
    }
    return YES; // handle the touch
}
于 2013-07-16T12:59:57.247 に答える
0

ジェスチャ認識はツールバーに設定されているため、送信者は常にツールバーになります。つまり、

if(sender.view.tag==1) 

この状態は機能しません。代わりにできることは、 UIView hitTest:withEventメソッドを使用するか、すべてのボタンに 1 つのアクション/セレクターを使用することです。その後、個々のボタンのタグを使用して、どのバー ボタンがそのアクションを呼び出したかを知ることができます。

[buttonGoBack setAction:@selector(buttonTapped:)]
[fixedSpace setAction:@selector(buttonTapped:)];
[buttonGoForward setAction:@selector(buttonTapped:)];

- (IBAction)buttontapped:(id)sender{
if(sender.tag == 0)
{
  // perform some action
}
else if(sender.tag == 1){
  // perform some action
}
}
于 2013-07-16T11:01:00.920 に答える