1.由于最近有点忙,学习swift的计划有被耽搁了,到现在为止才做了30个小demo 的第四个。
2.废话不多说 直接计入正题
######1.效果和下图差不多
import UIKit
class ViewController: UIViewController {
lazy var scrollView = UIScrollView(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController {
func setupUI() {
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * 3, height: UIScreen.main.bounds.height)
scrollView.isPagingEnabled = true
scrollView.showsVerticalScrollIndicator = false;
scrollView.showsHorizontalScrollIndicator = false;
self.view.addSubview(scrollView);
let firstVc = FirstViewController()
firstVc.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
let secondVc = SecondViewController()
secondVc.view.frame = CGRect(x: UIScreen.main.bounds.width, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
let thirdVc = ThirdViewController()
thirdVc.view.frame = CGRect(x: UIScreen.main.bounds.width * 2, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
scrollView.addSubview(firstVc.view)
scrollView.addSubview(secondVc.view)
scrollView.addSubview(thirdVc.view)
}
}
复制代码
######3.第一个控制器
import UIKit
class FirstViewController: UIViewController {
lazy var imageView = UIImageView(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "image1.jpg")
self.view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
复制代码
######4.第二个控制器
import UIKit
class SecondViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController = UIImagePickerController()
struct Platform {
static let isSimulator: Bool = {
var isSim = false
#if arch(i386) || arch(x86_64)
isSim = true
#endif
return isSim
}()
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
print("\(UIImagePickerControllerSourceType.camera)")
if Platform.isSimulator {
// Do one thing
print("模拟器不能照相")
}
else {
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
}
view.addSubview(imagePicker.view)
self.present(imagePicker, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
复制代码
######5.第三个控制器
import UIKit
class ThirdViewController: UIViewController {
lazy var imageView = UIImageView(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "image3.jpg")
self.view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
复制代码
#####6.说明: 1,这个主要是一个scrollview的滑动视图,然后里面放3个控制器,第一个控制器和第三个控制器放的是界面,第二个控制器放的就是系统相机