iOS开发——UI篇Swift篇&UIScrollView
UIScrollView
1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 //创建滚动视图 8 var mainScrollView:UIScrollView! 9 10 var view1:UIView! 11 var view2:UIView! 12 var view3:UIView! 13 14 //创建UIPageControl实例 15 var pageCtrl:UIPageControl! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 titleLabel.text = titleString 21 22 23 // Do any additional setup after loading the view. 24 25 //创建滚动视图 26 mainScrollView = UIScrollView(frame: CGRectMake(20, 100, 300, 400)) 27 28 //设置背景色 29 mainScrollView.backgroundColor = UIColor.grayColor() 30 31 //添加到视图上 32 self.view.addSubview(mainScrollView) 33 34 //创建3个视图,添加到ScrollView上 35 view1 = UIView(frame: CGRectMake(0, 0, 300, 400)) 36 view1.backgroundColor = UIColor.redColor() 37 mainScrollView.addSubview(view1) 38 39 40 view2 = UIView(frame: CGRectMake(300, 0, 300, 400)) 41 view2.backgroundColor = UIColor.greenColor() 42 mainScrollView.addSubview(view2) 43 44 view3 = UIView(frame: CGRectMake(600, 0, 300, 400)) 45 view3.backgroundColor = UIColor.blueColor() 46 mainScrollView.addSubview(view3) 47 48 //设置scrollView的容器大小 49 mainScrollView.contentSize = CGSizeMake(300*3, 400) 50 51 //设置滚动条风格 52 mainScrollView.indicatorStyle = UIScrollViewIndicatorStyle.White 53 54 55 56 //设置水平方向滚动条是否可见 57 mainScrollView.showsHorizontalScrollIndicator = false 58 59 //设置垂直方向滚动条是否可见 60 mainScrollView.showsVerticalScrollIndicator = false 61 62 //设置成翻页滚动 63 mainScrollView.pagingEnabled = true 64 65 mainScrollView.delegate = self 66 67 68 mainScrollView.maximumZoomScale = 2.0;//允许放大2倍 69 mainScrollView.minimumZoomScale = 0.5;//允许放大到0.5倍 70 71 72 73 //页面控制器 74 75 //创建UIPageControl实例 76 pageCtrl = UIPageControl(frame: CGRectMake(20, 540, 300, 40)) 77 78 //添加到视图 79 self.view.addSubview(pageCtrl) 80 81 //设置总页数 82 pageCtrl.numberOfPages = 3 83 84 //当前显示到第几页面 85 pageCtrl.currentPage = 0 86 87 88 //添加事件 89 pageCtrl.addTarget(self, action: "pageTurn:", forControlEvents: UIControlEvents.ValueChanged) 90 91 92 93 } 94 95 override func didReceiveMemoryWarning() { 96 super.didReceiveMemoryWarning() 97 // Dispose of any resources that can be recreated. 98 } 99 100 101 /* 102 // MARK: - Navigation 103 104 // In a storyboard-based application, you will often want to do a little preparation before navigation 105 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 106 // Get the new view controller using segue.destinationViewController. 107 // Pass the selected object to the new view controller. 108 } 109 */ 110 111 112 113 114 //MARK: - UIScrollViewDelegate 115 116 func scrollViewDidScroll(scrollView: UIScrollView) 117 { 118 //视图滚动中一直触发 119 } 120 121 func scrollViewWillBeginDragging(scrollView: UIScrollView) 122 { 123 //刚开始拖动视图时触发一次 124 } 125 126 func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 127 { 128 //将要结束拖动触发一次 129 } 130 131 func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) 132 { 133 //拖动结束,触发一次 134 } 135 136 func scrollViewWillBeginDecelerating(scrollView: UIScrollView) 137 { 138 //拖动接触后,开始减速 139 } 140 141 func scrollViewDidEndDecelerating(scrollView: UIScrollView) 142 { 143 //减速完毕,恢复到静止状态 144 145 //获取scrollView视图滚动的x坐标 146 var offX:CGFloat = scrollView.contentOffset.x 147 148 //计算当前是第几屏 149 var index:Int = (Int)(offX / 300) 150 151 //设置分页指示器currentPage值 152 pageCtrl.currentPage = index; 153 } 154 155 156 //缩放功能-返回缩放的视图 157 func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? 158 { 159 //获取当前滚动视图移动的X坐标 160 var offsetX = scrollView.contentOffset.x 161 println(offsetX) 162 163 164 if offsetX == 0 //如果没有滚动,返回view1 165 { 166 return view1 167 } 168 else if offsetX == 300 //滚动300,返回view2 169 { 170 return view2 171 } 172 else if offsetX == 600 //滚动600,返回view3 173 { 174 return view3 175 } 176 177 return nil 178 } 179 180 181 func scrollViewDidZoom(scrollView: UIScrollView) 182 { 183 //缩放过程中一直调用 184 } 185 186 func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView!) 187 { 188 //开始缩放,调用一次 189 } 190 191 func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat) 192 { 193 //缩放结束,调用一次 194 } 195 196 197 func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) 198 { 199 println("scrollViewDidEndScrollingAnimation") 200 } 201 202 func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool 203 { 204 return false 205 } 206 207 func scrollViewDidScrollToTop(scrollView: UIScrollView) 208 { 209 println("scrollViewDidScrollToTop") 210 } 211 212 213 214 //MARK: - 215 216 //分页控制器value改变事件 217 func pageTurn(sender:AnyObject?) 218 { 219 //获取分页控制器 220 // var newPageCtl = sender as UIPageControl 221 222 //2015年5月2后修改 223 var newPageCtl = sender as! UIPageControl 224 225 226 //获取分页控制器当前页 227 var index = newPageCtl.currentPage 228 229 //设置ScrollView的x移动距离 230 mainScrollView.contentOffset = CGPointMake((CGFloat)(300 * index), 0) 231 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。