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 //什么时候需要滚动:当我们内容区域。大于可现区域的时候,为了看到更多内容,才需要滚动去查看。 //创建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) //设置scrollView内容区域大小 scrollView.contentSize=CGSize(width: kScreenWidth*3, height: kScreeHeight*2) //设置scrollView 的偏移量 //scrollView.contentOffset=CGPoint(x: kScreenWidth, y: 0) //设置滚动条的样式 scrollView.indicatorStyle = .white //设置是否显示滚动条 //垂直滚动条 scrollView.showsVerticalScrollIndicator=false scrollView.showsHorizontalScrollIndicator=false //方向锁,滚动时候只能朝一个方向滚动 scrollView.isDirectionalLockEnabled=true //设置是否有弹簧xiaoguo //scrollView.bounces=false //设置水平方向有弹簧效果 scrollView.alwaysBounceHorizontal=true //设置垂直方向有弹簧效果 scrollView.alwaysBounceVertical=true //设置是否支持整页滚动 scrollView.isPagingEnabled=true //设置scrollView是否支持滚动 scrollView.isScrollEnabled=true scrollView.scrollsToTop=true //代理属性 //设置是否支持回到顶部 scrollView.delegate=self //设置最小缩放比例 scrollView.minimumZoomScale=1.0 //设置最大缩放比例 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的协议方法extension ViewController:UIScrollViewDelegate{ //1 当ScrollView滚动的时候这个方法会持续触发 func scrollViewDidScroll(_ scrollView: UIScrollView) {// any offset changes print("滚动着,滚动着") print(scrollView.contentOffset) } //2缩放过程中持续触发 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开始拖拽时候触发 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将要结束拖拽的时候触发 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer){
print("将要结束")
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
}
//5已经结束拖拽时候触发
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
print("结束拖拽")
}
//6将要开始减速的时候触发
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设置一个结束动画的时候触发,不制定动画就不会触发
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将要开始缩放的时候触发
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?){ // called before the scroll view begins zooming its content
}
//11结束缩放的时候触发
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {// scale between minimum and maximum. called after any 'bounce' animations
}
//12设置点击状态栏是否能回到顶部
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {// return a yes if you want to scroll to the top. if not defined, assumes YES
return true
}
//13scrollView回到顶部触发的方法
func scrollViewDidScrollToTop(_ scrollView: UIScrollView){
print("scrllView已经回到顶部")
}
}
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)
//设为主屏幕,以及使其可见
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:.
}
}