Swift- 视图和手势

1.UIView的层次关系
let rect = CGRectMake(30, 50, 200, 200)
let view = UIView(frame: rect)
view.backgroundColor = UIColor.brownColor()
self.view.addSubview(view)

let viewSub = UIView(frame:CGRectMake(10,10,100,100))
viewSub.backgroundColor = UIColor.blueColor()
view.addSubview(viewSub)

2.给图像视图添加边框效果
let image = UIImage(named: “1”)
let imageView = UIImageView(image: image)

imageView.frame = CGRectMake(24, 80, 270, 410)
//设置边框宽度
imageView.layer.borderWidth = 10
//设置边框颜色
imageView.layer.borderColor = UIColor.blueColor().CGColor

self.view.addSubview(imageView)

3.给图像视图添加圆角效果
let image = UIImage(named: “1”)
let imageView = UIImageView(image: image)

imageView.frame = CGRectMake(24, 80, 300, 300)
//设置视图半径
imageView.layer.cornerRadius = 150
//对视图进行剪切
imageView.layer.masksToBounds = true

self.view.addSubview(imageView)

4.给图像视图添加阴影效果
let image = UIImage(named: “1”)
let imageView = UIImageView(image: image)

imageView.frame = CGRectMake(24, 80, 300, 300)
//设置图像视图层的阴影颜色为黑色
imageView.layer.shadowColor = UIColor.blackColor().CGColor
//设置图像视图层,阴影的横向和纵向偏移值
imageView.layer.shadowOffset = CGSizeMake(30.0, 30.0)
//设置图像视图层的阴影透明度
imageView.layer.shadowOpacity = 0.45
//设置图像视图层的阴影半径大小
imageView.layer.shadowRadius = 5.0
self.view.addSubview(imageView)

5.UIView视图的渐变填充
let rect = CGRectMake(60, 200, 200, 200)
let gradientView = UIView(frame: rect)

//新建一个渐变层
let gradientLayer = CAGradientLayer()
//设置渐变层的位置和尺寸,与视图对象保持一致
gradientLayer.frame = gradientView.frame
//设置渐变的起始颜色为黄色
let fromColor = UIColor.yellowColor().CGColor
//设置渐变的中间颜色为红色
let midColor = UIColor.redColor().CGColor
//设置渐变的结束颜色为紫色
let toColor = UIColor.purpleColor().CGColor
//将渐变层的颜色数组属性,设置为由三个颜色构建的数组
gradientLayer.colors = [fromColor,midColor,toColor]
//将配置好的渐变层,添加到视图对象的层中
view.layer.addSublayer(gradientLayer)
self.view.addSubview(gradientView)

6.UIView视图的纹理填充
let image = UIImage(named: “1”)
//新建一个颜色对象,并将导入的图片赋予该对象
let patternColor = UIColor.init(patternImage: image!)
//将此颜色对象,赋值给当前根视图的背景
self.view.backgroundColor = patternColor

7. CGAffineTransform仿射变换的使用
let rect = CGRectMake(50, 150, 200, 50)
let view = UIView(frame:rect)
view.backgroundColor = UIColor.brownColor()
self.view.addSubview(view)

//创建一个仿射变换变量,仿射变换可以用于平移、旋转、缩放变换路径或者图形上下文
var transform = view.transform
//使用旋转功能,对视图进行45度旋转
transform = CGAffineTransformRotate(transform,3.14/4)
//将变换变量,赋值给视图对象
view.transform = transform
8.单击手势
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRectMake(32, 80, 256, 256)
let imageView = UIImageView(frame: rect)

let image = UIImage(named: “1”)
imageView.image = image
//开启图像视图的交互功能
imageView.userInteractionEnabled = true
//将图像视图添加到当前视图控制器的根视图
self.view.addSubview(imageView)

//创建一个手势检测类,这是一个抽象类,它定义了所有手势的基本行为,并拥有6个子类,来检测发生在设备中的各种手势
let gesture = UITapGestureRecognizer(target: self, action: “singleTap”)
//将创建的手势,指定给图像视图
imageView.addGestureRecognizer(gesture)

}

//创建用于接收手势事件的方法
func singleTap(){
//当接收到手势事件后,弹出一个提示窗口
let alertView = UIAlertController(title: “Infomation”, message: “single tap”, preferredStyle: UIAlertControllerStyle.Alert)
//创建一个按钮,作为提示窗口中的【确定按钮】。当用户点击该按钮时,将关闭提示窗口。
let OKAction = UIAlertAction(title: “OK”, style: .Default) { (ACTION) -> Void in

}
//将确定按钮添加到提示窗口中
alertView.addAction(OKAction)
//在当前视图控制器中,展示提示窗口
self.presentViewController(alertView, animated: true, completion: nil)
}

9.长按手势
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRectMake(32, 80, 256, 256)
let imageView = UIImageView(frame: rect)
let image = UIImage(named: “1”)
imageView.image = image

imageView.userInteractionEnabled = true
self.view.addSubview(imageView)
//创建一个长按手势对象,用于检测发生在设备中的长按手势
let gesture = UILongPressGestureRecognizer(target: self, action: “longPress:”)
//将手势指定给图像视图对象
imageView.addGestureRecognizer(gesture)
}
//创建一个方法,用来接收长按手势事件
func longPress(gesture:UILongPressGestureRecognizer){
//首先检测一下手势事件的阶段
if(gesture.state == UIGestureRecognizerState.Began){

let alertView = UIAlertController(title: “Infomation”, message: “Long Press”, preferredStyle: UIAlertControllerStyle.Alert)

let OKAction = UIAlertAction(title: “OK”, style: .Default) { (action) in

}
alertView.addAction(OKAction)
self.presentViewController(alertView, animated:true, completion:nil)
}
}

10.双击手势
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRectMake(32, 80, 256, 256)
let imageView = UIImageView(frame: rect)

let image = UIImage(named: “1”)
imageView.image = image
//开启图像视图的交互功能
imageView.userInteractionEnabled = true
//将图像视图添加到当前视图控制器的根视图
self.view.addSubview(imageView)

//创建一个手势检测类,这是一个抽象类,它定义了所有手势的基本行为,并拥有6个子类,来检测发生在设备中的各种手势
let gesture = UITapGestureRecognizer(target: self, action: “doubleTap”)
//设置点击次数为2,模拟双击事件
gesture.numberOfTapsRequired = 2
//设置手势为单击双次事件
gesture.numberOfTouchesRequired = 1
//将创建的手势,指定给图像视图
imageView.addGestureRecognizer(gesture)

}

//创建用于接收手势事件的方法
func doubleTap(){
//当接收到手势事件后,弹出一个提示窗口
let alertView = UIAlertController(title: “Infomation”, message: “doubel tap”, preferredStyle: UIAlertControllerStyle.Alert)
//创建一个按钮,作为提示窗口中的【确定按钮】。当用户点击该按钮时,将关闭提示窗口。
let OKAction = UIAlertAction(title: “OK”, style: .Default) { (ACTION) -> Void in

}
//将确定按钮添加到提示窗口中
alertView.addAction(OKAction)
//在当前视图控制器中,展示提示窗口
self.presentViewController(alertView, animated: true, completion: nil)
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注