Adding glow effect to UIButton - iOS

后端 未结 6 1988
一生所求
一生所求 2020-12-04 22:25

I have an UIButton which is a logo. This logo button will glow on forever but will stop glowing on touch.It is like a glowing animation.

Is there any suggestions?

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 23:17

    Glowing code taken from: Creating a Glow Effect for UILabel and UIButton

    First, you'll need to import the QuartzCore Framework:

    #import 
    

    When you create a button (or in viewDidLoad, depends on your code structure) add this code:

    UIColor *color = button.currentTitleColor;
    button.titleLabel.layer.shadowColor = [color CGColor];
    button.titleLabel.layer.shadowRadius = 4.0f;
    button.titleLabel.layer.shadowOpacity = .9;
    button.titleLabel.layer.shadowOffset = CGSizeZero;
    button.titleLabel.layer.masksToBounds = NO;
    

    You'll need to watch for two events: UIControlEventTouchDown and UIControlEventTouchUpInside

    In UIControlEventTouchDown handler you'll add the code:

    UIColor *color = [UIColor clearColor];
    button.titleLabel.layer.shadowColor = [color CGColor];
    

    And in UIControlEventUpInside handler you'll add the code:

    UIColor *color = button.currentTitleColor;
    button.titleLabel.layer.shadowColor = [color CGColor];
    

    Again details of implementation depend on whether you create button programmaticaly or via Interface Builder but i'm sure you'll be able to figure this out from here on.

    EDIT: for a custom button simply adding the following code should work:

    [button setImage:[UIImage imageNamed:@"buttonWithGlow.png"]
            forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"buttonWithNoGlow.png"] 
            forState:UIControlStateHighlighted];
    

提交回复
热议问题