定制UIButton的发光效果,类似于信息UIButton。

3

由于我是iPhone开发的新手。 我想给“自定义UIButton”添加类似于“info UIButton”的发光效果。

enter image description here --> enter image description here

请帮助我,在按钮点击时实现上述效果。


1
请查看这个链接,它会对你有所帮助!!! - ios_av
5个回答

15

我找到了一个简单的方法来实现我的要求。 具体做法是启用“在高亮时显示触摸”属性的复选框。 请参见以下截屏。

输入图片说明

对于我的自定义按钮,这个方法运行良好。


6

如果您不想使用图像,则可以使用以下代码 导入“CoreGraphics框架”以使用下面的代码

-(IBAction)myButtonTapped:(id)sender
{
 UIButton *tmpbtn=(UIButton *)sender;
 tmpbtn.layer.shadowColor = [UIColor cyanColor].CGColor;
 tmpbtn.layer.shadowRadius = 10.0f;
 tmpbtn.layer.shadowOpacity = 1.0f;
 tmpbtn.layer.shadowOffset = CGSizeZero;
}

要取消发光效果,只需添加:
-(void)removeBtnGlow
{
   myBtn.layer.shadowColor = [UIColor clearColor].CGColor;
   myBtn.layer.shadowRadius = 10.0f;
   myBtn.layer.shadowOpacity = 1.0f;
   myBtn.layer.shadowOffset = CGSizeZero;
}

1
作为一个UIButton类别,并使用glowWithProgress:,您可以动画地改变发光效果。
@implementation UIButton (Extra)

-(void)addGlow{
    [self glowWithProgress:1.0];
}

-(void)removeGlow{
    [self glowWithProgress:0.0];
}

-(void)glowWithProgress:(CGFloat)progress{
    CGFloat realProgress = MAX(.0f, MIN(1.0f, progress));
    self.layer.shadowColor = [UIColor whiteColor].CGColor;
    self.layer.shadowRadius = 10.0f;
    self.layer.shadowOpacity = 1.0f * realProgress;
    self.layer.shadowOffset = CGSizeZero;
}

@end

1
你需要剪切发光效果的图像,并使用以下代码。
[yourBtn setBackgroundImage:[UIImage imageNamed:@"yourimagename"] forState:UIControlStateHighlighted];

1

你尝试过创建一个发光版本的按钮图标,并在代码中进行设置吗?例如:

[yourButton setImage:glowImage forState:UIControlStateHighlighted];

或者在界面构建器中:

This is the default image for the button This would be how you set your glowing icon


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接