UIScrollVIew 滚动视图内容总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIScrollVIew 滚动视图内容总结相关的知识,希望对你有一定的参考价值。
1 - (void)viewDidLoad 2 3 { 4 5 [super viewDidLoad]; 6 7 // Do any additional setup after loading the view. 8 9 10 11 // 滚动视图 12 13 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)]; 14 15 scrollView.backgroundColor = [UIColor redColor]; 16 17 18 19 // 设定滚动内容的范围(CGSize) 20 21 scrollView.contentSize = CGSizeMake(2800, 0); // 横纵 坐标 22 23 // 关掉边缘的弹动搞效果 24 25 // srollView.bounces = NO; 26 27 // 偏移量(CGPoint) 28 29 // srollView.contentOffset = CGPointMake(-100, -100); 30 31 // 内容距离上 左 下 右边缘的距离 32 33 scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); 34 35 // 滑动到顶部 36 37 scrollView.scrollsToTop = YES; 38 39 // 整屏翻动 40 41 scrollView.pagingEnabled = YES; 42 43 // srollView能不能滚动 44 45 scrollView .scrollEnabled = YES; // NO:不能滚动 46 47 // 是否显示横向的滚动条 48 49 scrollView.showsHorizontalScrollIndicator = YES; 50 51 52 53 54 55 56 57 // 2.将viewController设置为ScrollView的 代理人 58 59 scrollView.delegate = self; 60 61 62 63 64 65 // scrollView的缩放 66 67 // 缩放:1. 设置scrollView的缩放范围 68 69 scrollView.minimumZoomScale = 0.5; // 最小范围 70 71 scrollView.maximumZoomScale = 2; // 最大范围 72 73 74 75 // 设置缩放时是否弹动 76 77 scrollView.bouncesZoom = YES; 78 79 80 81 [self.view addSubview:scrollView]; 82 83 [scrollView release]; 84 85 86 87 88 89 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 240, 400)]; 90 91 imageView.image = [UIImage imageNamed:@"1.JPG"]; 92 93 [scrollView addSubview:imageView]; 94 95 [imageView release]; 96 97 98 99 UIImageView *imageView0 = [[UIImageView alloc] initWithFrame:CGRectMake(300, 20, 240, 400)]; 100 101 imageView0.image = [UIImage imageNamed:@"2.JPG"]; 102 103 [scrollView addSubview:imageView0]; 104 105 [imageView0 release]; 106 107 108 109 UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)]; 110 111 imageView1.image = [UIImage imageNamed:@"3.JPG"]; 112 113 [scrollView addSubview:imageView1]; 114 115 [imageView1 release]; 116 117 118 119 UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)]; 120 121 imageView2.image = [UIImage imageNamed:@"4.JPG"]; 122 123 [scrollView addSubview:imageView2]; 124 125 [imageView2 release]; 126 127 128 129 UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(860, 20, 240, 400)]; 130 131 imageView3.image = [UIImage imageNamed:@"5.JPG"]; 132 133 [scrollView addSubview:imageView3]; 134 135 [imageView3 release]; 136 137 138 139 UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(1140, 20, 240, 400)]; 140 141 imageView4.image = [UIImage imageNamed:@"6.JPG"]; 142 143 [scrollView addSubview:imageView4]; 144 145 [imageView4 release]; 146 147 148 149 // UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(1420, 20, 240, 400)]; 150 151 // 152 153 // [scrollView addSubview:imageView5]; 154 155 // [imageView5 release]; 156 157 158 159 160 161 162 163 164 UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 420, 280, 62)]; 165 166 // pageControl.backgroundColor =[UIColor clearColor]; 167 168 169 170 // 显示多少个点点 171 172 pageControl.numberOfPages = 5; 173 174 pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; 175 176 177 178 // 给pageControl加一个响应方法 179 180 [pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged]; 181 182 183 184 [self.view addSubview:pageControl]; 185 186 [pageControl release]; 187 188 189 190 191 192 // 方便在其他方法中调用pageControl 193 194 self.page = pageControl; 195 196 } 197 198 199 - (void)pageControlAction:(UIPageControl *)pageControl 200 201 { 202 203 NSLog(@"%d", pageControl.currentPage); 204 205 } 206 207 // 缩放:2. 设置一个视图 随着scrollView放大缩小 208 209 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 210 211 { 212 213 return [scrollView.subviews firstObject]; // 把第一个子视图随着scrollView缩放 214 215 } 216 217 // 3. 实现相对应的代理方法(协议方法) 218 219 - (void)scrollViewDidSroll:(UIScrollView *)scrollView 220 221 { 222 223 // // 只要srollView滚动 就会一直触发这个方法 224 225 // NSLog(@"%s", __FUNCTION__); 226 227 // // srollView 的偏移量变化 228 229 // NSLog(@"偏移量变化: %@", NSStringFromCGPoint(scrollView.contentOffset)); 230 231 // 232 233 // // 判断scrollView当前的页数 234 235 // // 偏移量.x / srollView宽度 236 237 // int pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width; 238 239 // NSLog(@"页数: %d", pageNumber); 240 241 242 243 // 当前scrollView滚动到当前页数 244 245 self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width; 246 247 248 249 } 250 251 // 开始拖拽的时候 252 253 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 254 255 { 256 257 NSLog(@"开始拖拽:%s", __FUNCTION__); 258 259 } 260 261 // 结束拖拽的时候 262 263 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 264 265 { 266 267 NSLog(@"结束拖拽:%s", __FUNCTION__); 268 269 } 270 271 // 开始减速的时候 272 273 - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView 274 275 { 276 277 NSLog(@"开始减速:%s", __FUNCTION__); 278 279 } 280 281 // 结束减速的时候 282 283 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 284 285 { 286 287 NSLog(@"结束减速:%s", __FUNCTION__); 288 289 }
以上是关于UIScrollVIew 滚动视图内容总结的主要内容,如果未能解决你的问题,请参考以下文章