2013-03-06 1 views

답변

1

결국 UIButton을 하위 클래스로 분류했습니다. 다음은 구현 파일 코드입니다. 나는 약간의 응용 프로그램 특정 물건을했다, 그래서 나는이 정확한 코드를 테스트하지 않은,하지만 괜찮을한다 :

#import "SlowFadeButton.h" 

@interface SlowFadeButton() 

@property(strong, nonatomic)UIImageView *glowOverlayImgView; // Used to overlay glowing animal image and fade out 

@end 

@implementation SlowFadeButton 



-(id)initWithFrame:(CGRect)theFrame mainImg:(UIImage*)theMainImg highlightImg:(UIImage*)theHighlightImg 
{ 
    if((self = [SlowFadeButton buttonWithType:UIButtonTypeCustom])) { 

     self.frame = theFrame; 

     if(!theMainImg) { 
      NSLog(@"Problem loading the main image\n"); 
     } 
     else if(!theHighlightImg) { 
      NSLog(@"Problem loading the highlight image\n"); 
     } 

     [self setImage:theMainImg forState:UIControlStateNormal]; 
     self.glowOverlayImgView = [[UIImageView alloc] initWithImage:theHighlightImg]; 
     self.glowOverlayImgView.frame = self.imageView.frame; 
     self.glowOverlayImgView.bounds = self.imageView.bounds; 

     self.adjustsImageWhenHighlighted = NO; 
    } 

    return self; 
} 


-(void)setHighlighted:(BOOL)highlighted 
{ 
    // Check if button is going from not highlighted to highlighted 
    if(![self isHighlighted] && highlighted) { 
     self.glowOverlayImgView.alpha = 1; 
     [self addSubview:self.glowOverlayImgView]; 
    } 
    // Check if button is going from highlighted to not highlighted 
    else if([self isHighlighted] && !highlighted) { 
     [UIView animateWithDuration:1.0f 
         animations:^{ 
          self.glowOverlayImgView.alpha = 0; 
         } 
         completion:NULL]; 
    } 

    [super setHighlighted:highlighted]; 
} 

-(void)setGlowOverlayImgView:(UIImageView *)glowOverlayImgView 
{ 
    if(glowOverlayImgView != _glowOverlayImgView) { 
     _glowOverlayImgView = glowOverlayImgView; 
    } 

    self.glowOverlayImgView.alpha = 0; 
} 

@end 

당신은 또한 단지 [self imageForState:UIControlStateHighlighted]에서 강조 표시된 이미지를 끌어와 그것을 사용할 수 있습니다, 그것은 작동합니다 같은. 중요한 것은 adjustsImageWhenHighlighted = NO을 확인한 다음 setHighlighted: 메서드를 재정의하는 것입니다.

관련 문제