图片加载和添加手势事件
private lazy var imageV1 : UIImageView = {
let v1 = UIImageView()
v1.userInteractionEnabled = true
v1.layer.cornerRadius = 8 // 添加圆角
v1.layer.masksToBounds = true //裁剪超出部分
v1.image = UIImage(named: "detail.jpg")
v1.backgroundColor = UIColor.purpleColor()
var tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.photoOnlick))
v1.addGestureRecognizer(tap)
return v1
}()
func photoOnlick(){
print("-------")
}
按钮属性设置和点击事件
private lazy var btn:UIButton = {
let btn = UIButton()
btn.backgroundColor = UIColor.redColor()
btn.setTitle("关注", forState: UIControlState.Normal)
btn.setTitle("已关注", forState: UIControlState.Selected)
//非传递按钮点击事件
btn.addTarget(self, action:#selector(ViewController.tapped), forControlEvents:.TouchUpInside)
//传递按钮点击事件
btn.addTarget(self, action:#selector(tapped), forControlEvents:.TouchUpInside)
return btn
}()
@objc private func tapped(){
btn.selected = false
} //非传递按钮点击事件,前面添加@objc,当OC方法来处理
@objc private func tapped(btn:UIButton){
btn.selected = false
} //传递按钮点击事件,前面添加@objc,当OC方法来处理