2012-02-20 5 views
5

UIToolbarUIBarButtonItemをタップすると、白いグロー効果があります。UIBarButtonItemでハイライト効果を発生させる方法

イベントを発生させてこの効果を表示する可能性はありますか?

私はボタンを押したくありません。エフェクトのみを表示する必要があります。私は、このボタンの背後に新しいコンテンツがあることをユーザーに視覚化したいと思います。

ありがとうございました!バーの項目全般で

[(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:@"highlight.png"] forState:UIControlStateNormal]; 

については

答えて

2

次のメソッドは、ナビゲーションコントローラのtoolbarItemsプロパティを使用しており、強調表示するボタンがその配列の最後の項目であることを前提としています。もちろん、任意のツールバーに適用することができ、必要に応じて別の配列インデックスを選択することもできます。

今では、アニメーションによって新しいボタンが画面の左下から移動するため、これは完全には完璧ではありません。しかし、私はそれが少しの努力でオフにすることができると思います。

私はPeakJiの画像を使用しました。

私はこれがうまくいきたいと思います。

お楽しみください、

ダミアンあなたの答えのための

- (void)highlightButton { 
    NSArray *originalFooterItems = self.toolbarItems; 
    NSMutableArray *footerItems = [originalFooterItems mutableCopy]; 
    [footerItems removeLastObject]; 

    NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"white_glow" ofType:@"png"]; 
    UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage]; 
    imageView.frame = CGRectMake(0, 0, 40, 40); 

    UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView]; 
    [footerItems addObject:flashImage]; 

    [UIView animateWithDuration:0.3 
          delay: 0.0 
         options: UIViewAnimationOptionCurveEaseOut 
        animations:^{ 
         self.toolbarItems = footerItems;      } 
        completion:^(BOOL finished){ 
         [UIView animateWithDuration:.3 
               delay: 0.0 
              options:UIViewAnimationOptionCurveEaseIn 
              animations:^{ 
               self.toolbarItems = originalFooterItems; 
              } 
              completion:nil]; 
        }]; 
} 
1

- あなたが任意の時点で、プログラムでこの関数を呼び出すことができますが、ボタン

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button addTarget:self 
      action:@selector(someFunction:) 
forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"Click here" forState:UIControlStateNormal]; 
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0); 
[self.view addSubview:button]; 

を持っていると仮定すると:

[button setTitle:@"Look Here" forState:UIControlStateNormal]; 

場合や、ハイライト画像が好きです。

btnImage = [UIImage imageNamed:@"highlight.png"]; 
[button setImage:btnImage forState:UIControlStateNormal]; 

非常に単純な代替:

前記、あなたもこのようなボタンを設定することができますHERESに

- (void)highlightButton:(UIButton *)button { 
    [button setHighlighted:YES]; 
} 
+0

ありがとう!ツールバーのホワイトグロー効果は、iOS SDKではデフォルトです。ハイライトはありません.pngを作成する必要があります。デフォルトのホワイトグロー効果を発する他の方法はありませんか? – Manni

+0

@Manni私はあなたのためにhighlight.pngを作ったばかりです。LOL – PeakJi

4

"highlight.png" を、私は冗談ではありません! (あなたが白の背景にそれを見ていないかもしれないが。)

highlight.png screenshot

+8

彼は冗談ではないことを確認できます:) –

1

あなたは異なるスタイルで新しいバーボタンアイテムを再作成しようとし、それをバックに再割り当てすることができます。私の実験でした:

のviewDidLoadで

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom" 
                   style:UIBarButtonItemStylePlain 
                   target:self 
                   action:nil]; 
    self.navigationItem.rightBarButtonItem = rightBtn; 
    [rightBtn release]; 

    [self performSelector:@selector(highlight) 
       withObject:nil 
       afterDelay:2.0]; 
} 

そして、メソッドのハイライトに:

- (void) highlight{ 
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom" 
                   style:UIBarButtonItemStyleDone 
                   target:self 
                   action:nil]; 
    self.navigationItem.rightBarButtonItem = rightBtn; 
    [rightBtn release]; 
} 

あなたの実装では、限り、あなたはバーボタンを再割り当てすると罰金として私より異なることかもしれませんが、 、私はそれが期待どおりに動作すると思います。幸運:)

0

シンプルAYあなたはカテゴリがある場合のCustomView(UIBUtton)

UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[favButton setFrame:CGRectMake(0.0, 100.0, 50.0, 30.0)]; 
[favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_normal.png"] 
      forState:UIControlStateNormal]; 
[favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_hightlight.png"] 
      forState:UIControlStateHighlighted]; 
[favButton setTitle:@"Add" forState:UIControlStateNormal]; 
[favButton setTitle:@"Add" forState:UIControlStateHighlighted]; 
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 
[[favButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]]; 
[favButton addTarget:self action:@selector(doSomething) 
    forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:favButton]; 
1

であなたのUIBarButtonItemを初期化することです。

#define HIGHLIGHT_TIME 0.5f 

@interface UIButton (Highlight) 
- (void)highlight; 
@end 

@implementation UIButton (Highlight) 

- (void)highlight { 
    UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    overlayButton.frame = self.frame; 
    overlayButton.showsTouchWhenHighlighted = YES; 
    [self.superview addSubview:overlayButton]; 
    overlayButton.highlighted = YES; 
    [self performSelector:@selector(hideOverlayButton:) withObject:overlayButton afterDelay:HIGHLIGHT_TIME]; 
} 

- (void)hideOverlayButton:(UIButton *)overlayButton { 
    overlayButton.highlighted = NO; 
    [overlayButton removeFromSuperview]; 
} 

@end 

を次にあなたがこのようなコードが必要...

UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease]; 
self.navigationItem.rightBarButtonItem = addButton; 
[self.navigationController.navigationBar.subviews.lastObject performSelector:@selector(highlight) withObject:nil afterDelay:2]; 

... 2秒後にナビゲーションバーのボタンを強調表示します。

関連する問題