Animate UIButton state change

前端 未结 5 751
天涯浪人
天涯浪人 2020-12-12 21:00

I\'m using a UIButton with images for normal and highlighted states. They work as expected but I want to have some fading/merging transition and not just a sudden swap.

5条回答
  •  时光取名叫无心
    2020-12-12 21:09

    To accomplish that I extended UIButton. added a new property called hilightedImage with the following code:

    - (void)setHilightImage:(UIImageView *)_hilightImage
    {
        if (hilightImage != _hilightImage) {
            [hilightImage release];
            hilightImage = [_hilightImage retain];
        }
        [hilightImage setAlpha:0];
        [self addSubview:hilightImage];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.14];
        if(hilightImage){
            [hilightImage setAlpha:1];
        }
        [UIView commitAnimations];
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        self.highlighted = FALSE;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.14];
        if(hilightImage){
            [hilightImage setAlpha:0];
        }
    
        [UIView commitAnimations];
        [super touchesEnded:touches withEvent:event];
    }
    

提交回复
热议问题