import UIKitclass ViewController: UIViewController {? ? //獲取屏幕的寬? let kScreenWidth=UIScreen.main.bounds.size.width? ? //獲取屏幕的高? ? let kScreeHeight=UIScreen.main.bounds.size.height? ? override func viewDidLoad() {? ? ? ? super.viewDidLoad()? ? ? ? //UIScrollView:滾動視圖,是所有滾動視圖的基類,只要一個視圖能夠滾動立膛,要么是UIScrollView,要是UIScrollView的子類酷鸦,务冕。UIScrollView有倆個重要字類:UITableView,UICollectionView? ? ? ? //什么時候需要滾動:當(dāng)我們內(nèi)容區(qū)域袱耽。大于可現(xiàn)區(qū)域的時候死遭,為了看到更多內(nèi)容鞠眉,才需要滾動去查看澄峰。? ? ? ? //創(chuàng)建UIScrollView? ? ? ? let scrollView=UIScrollView(frame: CGRect(x: 20, y: 20, width: kScreenWidth-40, height: kScreeHeight-40))? ? ? ? scrollView.backgroundColor=#colorLiteral(red: 1, green: 0.9047565644, blue: 0.9592488978, alpha: 1)? ? ? ? //設(shè)置scrollView內(nèi)容區(qū)域大小? ? ? ? scrollView.contentSize=CGSize(width: kScreenWidth*3, height: kScreeHeight*2)? ? ? ? ? ? ? //設(shè)置scrollView 的偏移量? ? ? ? //scrollView.contentOffset=CGPoint(x: kScreenWidth, y: 0)? ? ? ? //設(shè)置滾動條的樣式? ? ? ? scrollView.indicatorStyle = .white? ? ? ? //設(shè)置是否顯示滾動條? ? ? ? //垂直滾動條? ? ? ? scrollView.showsVerticalScrollIndicator=false? ? ? ? scrollView.showsHorizontalScrollIndicator=false? ? ? ? //方向鎖嫉沽,滾動時候只能朝一個方向滾動? ? ? ? scrollView.isDirectionalLockEnabled=true? ? ? ? //設(shè)置是否有彈簧xiaoguo? ? ? ? //scrollView.bounces=false? ? ? ? //設(shè)置水平方向有彈簧效果? ? ? ? scrollView.alwaysBounceHorizontal=true? ? ? ? //設(shè)置垂直方向有彈簧效果? ? ? ? scrollView.alwaysBounceVertical=true? ? ? ? //設(shè)置是否支持整頁滾動? ? ? ? scrollView.isPagingEnabled=true? ? ? ? //設(shè)置scrollView是否支持滾動? ? ? ? scrollView.isScrollEnabled=true? ? ? ? scrollView.scrollsToTop=true? ? ? ? //代理屬性? ? ? ? //設(shè)置是否支持回到頂部? ? ? ? scrollView.delegate=self? ? ? ? //設(shè)置最小縮放比例? ? ? ? scrollView.minimumZoomScale=1.0? ? ? ? //設(shè)置最大縮放比例? ? ? ? scrollView.maximumZoomScale=3? ? ? self.view.addSubview(scrollView)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? let imageView=UIImageView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 2*kScreeHeight))? ? ? ? imageView.image=#imageLiteral(resourceName: "image.jpg")? ? ? ? imageView.tag=200? ? ? ? scrollView.addSubview(imageView)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }}//在延展中管理UIScrollViewDelegate的協(xié)議方法extension ViewController:UIScrollViewDelegate{? //1? 當(dāng)ScrollView滾動的時候這個方法會持續(xù)觸發(fā)? ? func scrollViewDidScroll(_ scrollView: UIScrollView) {// any offset changes? ? ? ? print("滾動著,滾動著")? ? ? ? print(scrollView.contentOffset)? ? }? ? //2縮放過程中持續(xù)觸發(fā)? ? func scrollViewDidZoom(_ scrollView: UIScrollView) {// any zoom scale changes? print("縮放著俏竞,縮放著")? ? ? ? print(scrollView.zoomScale)? ? ? ? // called on start of dragging (may require some time and or distance to move)? ? }? ? //3開始拖拽時候觸發(fā)? ? func scrollViewWillBeginDragging(_ scrollView: UIScrollView){? ? print("開始拖拽了")? ? // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest? ? }? ? //4將要結(jié)束拖拽的時候觸發(fā)? ? func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer){
print("將要結(jié)束")
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
}
//5已經(jīng)結(jié)束拖拽時候觸發(fā)
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("結(jié)束拖拽")
}
//6將要開始減速的時候觸發(fā)
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
print("將要開始減速")
// called on finger up as we are moving
}
//7減速完成绸硕,速度為零,這個方法很重要魂毁,往往就是在這個方法獲取偏量
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {// called when scroll view grinds to a halt
print("減速完成呢")
}
//8給scrollView設(shè)置一個結(jié)束動畫的時候觸發(fā)玻佩,不制定動畫就不會觸發(fā)
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
}
//9返回scrollView縮放視圖
func viewForZooming(in scrollView: UIScrollView) -> UIView? {// return a view that will be scaled. if delegate returns nil, nothing happens
return scrollView.viewWithTag(200)
}
//10將要開始縮放的時候觸發(fā)
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?){ // called before the scroll view begins zooming its content
}
//11結(jié)束縮放的時候觸發(fā)
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {// scale between minimum and maximum. called after any 'bounce' animations
}
//12設(shè)置點(diǎn)擊狀態(tài)欄是否能回到頂部
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {// return a yes if you want to scroll to the top. if not defined, assumes YES
return true
}
//13scrollView回到頂部觸發(fā)的方法
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){
print("scrllView已經(jīng)回到頂部")
}
}
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window=UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor=#colorLiteral(red: 1, green: 0.3980397582, blue: 0.7863847613, alpha: 1)
//設(shè)為主屏幕,以及使其可見
self.window?.makeKeyAndVisible()
self.window?.rootViewController=ViewController()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}