1. 文件管理(NSFileManager)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//文件管理器
let fileManager = NSFileManager.defaultManager()
//设置路径
let path = NSHomeDirectory() + "/Documents/1.plist"
print(path)
if fileManager.fileExistsAtPath(path){
try! fileManager.removeItemAtPath(path)
}else{
let h = "happy day"
//创建plist文件
fileManager.createFileAtPath(path, contents: h.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true), attributes: nil)
}
let file = NSHomeDirectory() + "/Documents/2/3/4"
//创建文件夹
try! fileManager.createDirectoryAtPath(file, withIntermediateDirectories: true, attributes: nil)
let a = try! fileManager.attributesOfItemAtPath(path)
print(a[NSFileSize])
}
}
2. 文件处理器(NSFileHandle)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let fileManager = NSFileManager.defaultManager()
let path = NSHomeDirectory() + "/Documents/1.plist"
print(path)
if fileManager.fileExistsAtPath(path){
print("文件存在")
}else{
let h = "happy day"
fileManager.createFileAtPath(path, contents: h.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true), attributes: nil)
}
//设置文件处理器(可写,可读,可更新)
let handle = NSFileHandle.init(forReadingAtPath: path)
//设置读取文件数据的长度(默认每一次执行都是从0开始读取数据)
let d1 = handle?.readDataOfLength(2)
print(d1?.length)
print(NSString(data: d1!, encoding: NSUTF8StringEncoding))
let hadle1 = NSFileHandle.init(forWritingAtPath: path)
let s = "tu s"
//string -> NSData
let d = s.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
hadle1?.writeData(d!)
print(NSString(data: d!, encoding: NSUTF8StringEncoding))
}
}
3. Scrollview
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
var scrollview : UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
//设置scrollview
scrollview = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 200))
scrollview.backgroundColor = UIColor.brownColor()
self.view.addSubview(scrollview)
//设置textview
let textview = UITextView(frame: CGRect(x: 0, y: 0, width: scrollview.frame.size.width, height: scrollview.frame.size.height/2))
textview.backgroundColor = UIColor.redColor()
scrollview.addSubview(textview)
//关联方法
scrollview.delegate = self
//开启翻页
scrollview.pagingEnabled = true
//设置偏移时整个视图位置大小
scrollview.contentSize = CGSize(width: self.view.frame.size.width * 4, height: self.view.frame.size.height)
//开启方向锁(一次只能一个方向)
scrollview.directionalLockEnabled = true
//弹性效果
scrollview.bounces = false
scrollview.showsHorizontalScrollIndicator = false
scrollview.showsVerticalScrollIndicator = false
//创建分页对象
let page = UIPageControl()
//设置分页数目
page.numberOfPages = 3
page.frame = CGRect(x: scrollview.frame.size.width/2-50, y: scrollview.frame.size.height, width: 100, height: 100)
page.currentPageIndicatorTintColor = UIColor.blackColor()
page.addTarget(self, action: #selector(didchange(_:)), forControlEvents: .ValueChanged)
self.view.addSubview(page)
}
func didchange(sender: UIPageControl){
//设置偏移位置
let rect = CGPoint(x: scrollview.frame.size.width * CGFloat(sender.currentPage), y: 0)
//设置偏移动画
scrollview.setContentOffset(rect, animated: true)
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
print("开始拖动")
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("完成拖动")
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("开始减速")
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
print("停止减速")
}
}