12

UIImage[ を使用して UIButton を設定myButton setImage:forState:]; し、それcontentModeを usingに設定しまし[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit]; たが、ボタンをタップするUIViewContentModeScaleToFillと、元に戻って画像が引き伸ばされます。

を使用adjustsImageWhenHighlightedするとこれが修正されますが、維持したい暗くなる効果が失われます。

これに対処する方法について何か提案はありますか?

4

3 に答える 3

4
UIButton *imageBtn = [UIButton ...
imageBtn.adjustsImageWhenHighlighted = NO;

[imageBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDown];
[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDragEnter];
    [imageBtn addTarget:self action:@selector(doCancelHighlighted:) forControlEvents:UIControlEventTouchDragExit];

-(void)doSomething:(UIButton *)button{
    ...
    [self performSelector:@selector(doCancelHighlighted:) withObject:button afterDelay:0.2f];
}

-(void)doHighlighted:(UIButton *)button{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 300, 300)];
    imageView.backgroundColor = [UIColor blackColor];
    imageView.alpha = 0.7;
    imageView.tag = 1000;
    [button addSubview:imageView];
}

-(void)doCancelHighlighted:(UIButton *)button{
    UIView *view = [button subviewWithTag:1000];
    [UIView animateWithDuration:0.2f animations:^{
        view.alpha = 0;
    } completion:^(BOOL finished) {
        [view removeFromSuperview];        
    }];
}
于 2011-10-12T10:16:27.320 に答える
2

この問題に対する私の解決策(効率的ではないかもしれませんが、ハイライト効果をカスタマイズする機会が与えられます。標準のものよりも見栄えが良いと思います)は次のとおりです。

  • もちろんUIButtonをサブクラス化します。
  • プロパティ @property を追加 (非アトミック、保持) UIView *highlightView;
  • 画像を設定する方法(私の方法では、adjustsImageWhenHighlighted プロパティを設定するために重要な別のモードを使用できます)

    [self setImage:image forState:UIControlStateNormal];
    [self setAdjustsImageWhenHighlighted:NO];
    
  • setHighlighted: メソッドを次のようにオーバーライドします。

    \- (void)setHighlighted:(BOOL)highlighted {
        if (!highlightView) {
            self.highlightView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
            self.highlightView.backgroundColor = [UIColor darkGrayColor];
            self.highlightView.alpha = 0.0;
            [self addSubview:self.highlightView];
        }
        if (highlighted) {
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            highlightView.alpha = 0.5;
            [UIView commitAnimations];
        }
        else {
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            highlightView.alpha = 0.0;
            [UIView commitAnimations];
        }
    }
    

これは私にとってはうまくいきますが、より良い方法があれば、それを知りたいです。

于 2011-04-04T11:53:13.163 に答える
0

画像を任意の状態に設定したにコンテンツモードを設定すると、何らかの理由で発生します。

画像を設定する前に、プログラムで、またInterfaceBuilderでもコンテンツモードを設定してください。IB でこれを修正するには、すべての状態に設定されているすべての画像を削除し、コンテンツ モードを設定してから、画像を元に戻してください。

それは私のためにそれを修正しました。

于 2013-02-27T11:45:26.207 に答える