...照片的属性
- 1 UIScrollView
- 2.设置区区的大小,设置偏移量
- 3.是否整页翻动,触壁反弹关掉
- 4.展示滑动条,关闭任何拖拽
- 5.放大
- 6.设置代理
- 7,设置开始拖拽,结束拖拽,开始减速,结束减速。实现放缩的方法
- 8加载图片
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// UIScrollView,
let scr = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
//设置内容区域大小
scr.contentSize = CGSize(width: self.view.frame.size.width*5, height:self.view.frame.size.height)
self.view.addSubview(scr)
//设置偏移量
scr.contentOffset = CGPoint(x: self.view.frame.size.width, y: 0)
scr.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
// //是否整页翻动
// scr.isPagingEnabled = true
// //触壁反弹关掉
// scr.bounces = true
//展示滑动条
scr.showsHorizontalScrollIndicator = true
//关闭任何拖拽
//scr.isScrollEnabled = false
self.loadImage(scr: scr)
scr.indicatorStyle = .black
//放大
scr.minimumZoomScale = 0.2
scr.maximumZoomScale = 2
//设置代理
scr.delegate = self
}
// func scrollViewDidScroll(_ scrollView: UIScrollView) {
// print("任何偏移都会执行")
// //获取偏移量
// print(scrollView.contentOffset)
// }
//开始拖拽
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("开始拖拽")
}
//结束拖拽
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("结束拖拽")
}
//开始减速
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
print("开始减速")
}
//结束减速
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print("结束减速")
}
//实现放缩的方法
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
for view in scrollView.subviews{
//判断子视图是什么类型
if view is UIImageView{
return view
}
}
return nil
}
//加载图片
func loadImage(scr:UIScrollView){
let imageV1 = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height:self.view.frame.size.height))
imageV1.image = UIImage(named: "2.jpg")
scr.addSubview(imageV1)
let imageV2 = UIImageView(frame: CGRect(x: self.view.frame.size.width, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
imageV2.image = UIImage(named: "3.jpg")
scr.addSubview(imageV2)
let imageV3 = UIImageView(frame: CGRect(x: self.view.frame.size.width*2, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
imageV3.image = UIImage(named: "4.jpg")
scr.addSubview(imageV3)
let imageV4 = UIImageView(frame: CGRect(x: self.view.frame.size.width*3, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
imageV4.image = UIImage(named: "5.jpeg")
scr.addSubview(imageV4)
let imageV5 = UIImageView(frame: CGRect(x: self.view.frame.size.width*4, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
imageV5.image = UIImage(named: "6.jpg")
scr.addSubview(imageV5)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}