美文网首页
02 UIButton 和 UIImageView

02 UIButton 和 UIImageView

作者: 专注_刻意练习 | 来源:发表于2017-04-17 12:02 被阅读0次

//UIButton和UIImageView常用属性和常用方法总结


//MARK: - UIButton常用属性和常用方法总结

//1,按钮的创建

//按钮有下面四种类型:

/*

UIButtonType.contactAdd“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果

UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果

UIButtonType.dystem:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果

UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果

UIButtonType.infoDark:为感叹号“!”圆形按钮

UIButtonType.infoLight:为感叹号“!”圆形按钮

*/

//1,创建一个ContactAdd类型的按钮

let button:UIButton = UIButton.init(type: UIButtonType.contactAdd)

//设置按钮位置和大小

button.frame = CGRect.init(x: 10, y: 10, width: 50, height: 50)

//设置按钮文字

/*

UIControlState.highlighted//触摸状态下的文字

UIControlState.disabled//禁用状态下的文字

*/

button.setTitle("按钮", for: UIControlState.normal)

//普通状态下文字的颜色

button.setTitleColor(UIColor.black,for: .normal)

//按钮文字阴影颜色的设置

button.setTitleShadowColor(UIColor.green,for:.normal)

self.view.addSubview(button);

//按钮文字图标的设置

//分别设置按钮在normal和selected状态下的图片,点击按钮时切换按钮的选中状态:

button.setImage(UIImage(named:"icon1"),for:.normal)

button.setImage(UIImage(named:"icon2"),for:.selected)

//使触摸模式下按钮也不会变暗

button.adjustsImageWhenHighlighted=false

//使禁用模式下按钮也不会变暗

button.adjustsImageWhenDisabled=false

//设置按钮背景图片

button.setBackgroundImage(UIImage(named:"background1"),for:.normal)

//按钮触摸点击事件响应

/*

常用的触摸事件类型:

TouchDown:单点触摸按下事件,点触屏幕

TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候

TouchDragInside:触摸在控件内拖动时

TouchDragOutside:触摸在控件外拖动时

TouchDragEnter:触摸从控件之外拖动到内部时

TouchDragExit:触摸从控件内部拖动到外部时

TouchUpInside:在控件之内触摸并抬起事件

TouchUpOutside:在控件之外触摸抬起事件

TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断

*/

//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号

button.addTarget(self, action: #selector(ViewController.buttontap(_:)), for: UIControlEvents.touchUpInside)

//分别设置按钮在normal和selected状态下的图片,点击按钮时切换按钮的选中状态:

//对于Custom定制类型按钮,代码可简化为

//let button:UIButton = UIButton.init(frame: CGRect.init(x: 10, y: 10, width: 50, height: 50))

func buttontap( _ button:UIButton){

//分别设置按钮在normal和selected状态下的图片,点击按钮时切换按钮的选中状态:

button.isSelected = !button.isSelected

}


//MARK: - UIImageView常用属性和常用方法总结

//1使用图像控件显示图片

let imageView=UIImageView(image:UIImage(named:"icon"))

imageView.frame=CGRect.init(x: 10, y: 10, width: 100, height: 100)

self.view.addSubview(imageView)

//改变图片

imageView.image=UIImage(named:"icon2")

//2从文件目录中获取图片

let path = Bundle.main.path(forResource: "apple", ofType: "png")

let pathImage = UIImage(contentsOfFile: path!)

let pathImageView = UIImageView(image:pathImage)

self.view.addSubview(pathImageView)

//3从网络地址获取图片

//定义NSURL对象

let url = NSURL(string: "http://hangge.com/blog/images/logo.png")

//从网络获取数据流

let data = NSData(contentsOf: url! as URL)

//通过数据流初始化图片

let urlImage = UIImage(data: data! as Data)

let urlimageView = UIImageView(image:urlImage);

self.view.addSubview(urlimageView)

//4使用图像控件实现动画播放

//UIImageView中提供了存储多张图片来创建动画的功能,具体做法是,在animationImages属性中设置一个图片数组,然后使用startAnimating方法开始动画,最后用stopAnimating方法停止动画。同时,使用animationDuration属性中可以设置动画每帧切换的速度(秒)。

/*

class ViewController: UIViewController {

var imageView:UIImageView!

override func viewDidLoad() {

super.viewDidLoad()

imageView=UIImageView()

imageView.frame=CGRectMake(10,10,100,100)

//设置动画图片

imageView.animationImages=[UIImage(named:"icon")!,UIImage(named:"icon2")!]

//设置每隔0.5秒变化一次

imageView.animationDuration=0.5

self.view.addSubview(imageView)

}

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

imageView.startAnimating()

}

override func viewWillDisappear(animated: Bool) {

super.viewWillAppear(animated)

imageView.stopAnimating()

}

}

*/

//5保持图片比例

//默认UIImageView会拉伸图片使其占满整个UIImageView,如果不想让图片变形,可以将ContentMode设置为Aspect Fit。

imageView.contentMode = .scaleAspectFit

相关文章

网友评论

      本文标题:02 UIButton 和 UIImageView

      本文链接:https://www.haomeiwen.com/subject/iuctzttx.html