設(shè)置屏幕效果

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:.

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末席楚,一起剝皮案震驚了整個濱河市咬崔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌烦秩,老刑警劉巖垮斯,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異只祠,居然都是意外死亡兜蠕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門抛寝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熊杨,“玉大人,你說我怎么就攤上這事盗舰【Ц” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵钻趋,是天一觀的道長郊霎。 經(jīng)常有香客問我,道長爷绘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任进倍,我火速辦了婚禮土至,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘猾昆。我一直安慰自己陶因,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布垂蜗。 她就那樣靜靜地躺著楷扬,像睡著了一般解幽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烘苹,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天躲株,我揣著相機(jī)與錄音,去河邊找鬼镣衡。 笑死霜定,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的廊鸥。 我是一名探鬼主播望浩,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼惰说!你這毒婦竟也來了磨德?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤吆视,失蹤者是張志新(化名)和其女友劉穎典挑,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體揩环,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搔弄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了丰滑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片顾犹。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖褒墨,靈堂內(nèi)的尸體忽然破棺而出炫刷,到底是詐尸還是另有隱情,我是刑警寧澤郁妈,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布浑玛,位于F島的核電站,受9級特大地震影響噩咪,放射性物質(zhì)發(fā)生泄漏顾彰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一胃碾、第九天 我趴在偏房一處隱蔽的房頂上張望涨享。 院中可真熱鬧,春花似錦仆百、人聲如沸厕隧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吁讨。三九已至髓迎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間建丧,已是汗流浹背排龄。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茶鹃,地道東北人涣雕。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像闭翩,于是被迫代替她去往敵國和親挣郭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容