swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)

自定义view

(一)常用的写法

//  自定义View

import UIKit

private let  KLMargin:CGFloat  = 10
private let  KLLabelHeight:CGFloat  = 30

class CustomView: UIView {

    // 闭包 类似oc中的block
    var buttonCallBack:(() -> ())?
    
    // 重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.orangeColor()
        let lable: UILabel = UILabel(frame: CGRectMake(KLMargin, KLMargin, KLScreenWidth - (2 * KLMargin), KLLabelHeight))
        lable.text = "我丫就是一label"
        lable.textAlignment = NSTextAlignment.Center
        lable.backgroundColor = UIColor.lightGrayColor()
        self.addSubview(lable)
        
        
        let button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        button.frame = CGRectMake(KLMargin, CGRectGetMaxY(lable.frame) + KLMargin, KLScreenWidth - (2 * KLMargin), KLLabelHeight)
        button.backgroundColor = UIColor.lightTextColor()
        button.setTitle("俺是个按钮啊", forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("buttonCllick:"), forControlEvents: UIControlEvents.TouchUpInside)
        button.layer.cornerRadius = 5
        button.clipsToBounds = true
        self.addSubview(button)
        
    }

    // 反正重写了init方法这个会根据提示自动蹦出来
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 按钮点击事件的调用
    func buttonCllick(button: UIButton){
        if buttonCallBack != nil {
            buttonCallBack!()
        }
    }
    
    // 重新绘制和oc里面效果一样(其实我也不是很明白)
    override func drawRect(rect: CGRect) {
        //self.backgroundColor = UIColor.whiteColor()
    }

}
在其他类的调用
let  customView:CustomView = CustomView(frame: CGRectMake(0, 80, KLScreenWidth, KLScreenWidth/2))
                // 闭包(block)的回调
                customView.buttonCallBack = { () -> () in
                    customView.removeFromSuperview()
                }
                self.view.addSubview(customView)

(二)在一开始的时候,我是写在drawRect里的,并没有重写init方法,发现也能实现效果
override func drawRect(rect: CGRect) {
        self.backgroundColor = UIColor.orangeColor()
        let lable: UILabel = UILabel(frame: CGRectMake(KLMargin, KLMargin, KLScreenWidth - (2 * KLMargin), KLLabelHeight))
        lable.text = "我丫就是一label"
        lable.textAlignment = NSTextAlignment.Center
        lable.backgroundColor = UIColor.lightGrayColor()
        self.addSubview(lable)
        
        
        let button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        button.frame = CGRectMake(KLMargin, CGRectGetMaxY(lable.frame) + KLMargin, KLScreenWidth - (2 * KLMargin), KLLabelHeight)
        button.backgroundColor = UIColor.lightTextColor()
        button.setTitle("俺是个按钮啊", forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("buttonCllick:"), forControlEvents: UIControlEvents.TouchUpInside)
        button.layer.cornerRadius = 5
        button.clipsToBounds = true
        self.addSubview(button)
    }
在其他类调用的时候,无法调用CustomView(frame:rect) 这个方法,只有像下面的代码那样调用

let  customView:CustomView = CustomView()
                customView.backgroundColor = UIColor.orangeColor()
                customView.frame = CGRectMake(0, 80, KLScreenWidth, KLScreenWidth/2)
                customView.buttonCallBack = { () -> () in
                    customView.removeFromSuperview()
                }
                self.view.addSubview(customView)

其实功能都能实现,但是毕竟drawRect只是绘制机制,控件的初始化,就不要在drawRect搞了,还是在init方法初始化吧

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值