在一个按钮的图象与画圈子

时间:2015-06-25 07:51:02

标签: swift drawimage

我有一个显示图像的按钮。图像(圆圈中的图像为圆圈外的透明色的png)呈圆形,按钮是透明的。当我在屏幕上绘制按钮时,我只能看到带圆圈的按钮。 到目前为止,此工作正常。

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

我的功能创建按钮:

  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);


    }

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

1 个答案:

答案 0 :(得分:0)

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