gpt4 book ai didi

swift - 绘制圆圈的按钮上的图像

转载 作者:行者123 更新时间:2023-11-30 14:16:54 25 4
gpt4 key购买 nike

我有一个可以显示图像的按钮。图像(圆形中的图像,圆形外有透明颜色的 png)位于圆形中,按钮是透明的。当我在屏幕上绘制按钮时,我只能看到圆圈按钮。到目前为止效果很好。

现在我想在运行时在图像周围画一个黑色圆圈。关于如何做到这一点有任何提示吗?

我的函数创建按钮:

  func draw_button(sImage:String,X:CGFloat,Y:CGFloat, iTag:Int){

// Back Button
var sImagename=sImage;
var fButtonx:CGFloat=X;
var fButtony:CGFloat=Y;

var thebutton=UIButton(frame: CGRectMake(fButtonx, fButtony, iButtonSize, iButtonSize));
thebutton.addTarget(self, action: "buttonActionBotton:", forControlEvents: UIControlEvents.TouchUpInside)
thebutton.tag = iTag;
var image = UIImage(named: sImagename);


thebutton.setImage(image, forState: .Normal)
var transparent:UIColor=UIColor(red: 0, green: 0, blue: 0, alpha: 0.0);
thebutton.backgroundColor=transparent;
thebutton.layer.backgroundColor=transparent.CGColor;
self.view.addSubview(backButton);


}

关于我如何做到这一点有任何提示吗?

最佳答案

您可以使用位于按钮下方的 subview 。将其背景设置为黑色,并使用其图层cornerRadius属性来实现圆形。

let circleSubview = UIView(frame: CGRectMake(fButtonx, fButtony, iButtonSize, iButtonSize);
circleSubview.backgroundColor = UIColor.blackColor();
circleSubview.layer.cornerRadius = //whatever you need; I believe, it should be something around 10.0 or more - try it yourself


//after backButton added to view hierarchy
self.view.insertSubview(view: circleSubview, belowSubview : backButton)

一些建议 - 你不需要你的观点是变量。使用 let 代替 var。并且您有类函数 UIColor.clearColor() 而不是自己创建透明颜色。

关于swift - 绘制圆圈的按钮上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31044346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com