按钮上的图像画了一个圆圈

Image on a button with drawing a circle

我有显示图像的按钮。图像(圆圈中的图像为 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);


    }

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

您可以使用位于按钮正下方的子视图。 将其背景设置为黑色,并使用其层 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。而且你有 class 功能 UIColor.clearColor() 而不是自己创建透明颜色。