UI 运用滚动视图创建相册的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UI 运用滚动视图创建相册的方法相关的知识,希望对你有一定的参考价值。
1 #import "mainViewController.h" 2 3 4 @interface mainViewController () 5 6 7 @end 8 9 10 @implementation mainViewController 11 12 13 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 14 15 { 16 17 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 18 19 if (self) { 20 21 // Custom initialization 22 23 } 24 25 return self; 26 27 } 28 29 30 - (void)viewDidLoad 31 32 { 33 34 [super viewDidLoad]; 35 36 // Do any additional setup after loading the view. 37 38 //总的 39 40 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 450)]; 41 42 scrollView.backgroundColor = [UIColor redColor]; 43 44 scrollView.tag = 1000; 45 46 scrollView.contentSize = CGSizeMake(1400, 0); 47 48 scrollView.scrollEnabled = YES; 49 50 scrollView.pagingEnabled = YES; 51 52 //第一个 53 54 UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 240, 450)]; 55 56 scrollView1.contentSize = CGSizeMake(240, 0); 57 58 scrollView1.tag = 111; 59 60 UIImageView *image0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)]; 61 62 image0.image = [UIImage imageNamed:@"1.JPG"]; 63 64 [scrollView1 addSubview:image0]; 65 66 [image0 release]; 67 68 scrollView1.delegate = self; 69 70 scrollView1.minimumZoomScale = 0.5; 71 72 scrollView1.maximumZoomScale = 2; 73 74 [scrollView addSubview:scrollView1]; 75 76 [scrollView1 release]; 77 78 //第二个 79 80 UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(300, 20, 240, 450)]; 81 82 scrollView2.contentSize = CGSizeMake(280, 0); 83 84 scrollView2.tag = 222; 85 86 UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)]; 87 88 image1.image = [UIImage imageNamed:@"2.JPG"]; 89 90 [scrollView2 addSubview:image1]; 91 92 [image1 release]; 93 94 95 96 scrollView2.delegate = self; 97 98 scrollView2.minimumZoomScale = 0.5; 99 100 scrollView2.maximumZoomScale = 2; 101 102 [scrollView addSubview:scrollView2]; 103 104 [scrollView2 release]; 105 106 //第三个 107 108 UIScrollView *scrollView3 = [[UIScrollView alloc] initWithFrame:CGRectMake(580, 20, 240, 450)]; 109 110 scrollView3.contentSize = CGSizeMake(280, 0); 111 112 scrollView3.tag = 333; 113 114 UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)]; 115 116 image2.image = [UIImage imageNamed:@"3.JPG"]; 117 118 [scrollView3 addSubview:image2]; 119 120 [image2 release]; 121 122 123 scrollView3.delegate = self; 124 125 scrollView3.minimumZoomScale = 0.5; 126 127 scrollView3.maximumZoomScale = 2; 128 129 [scrollView addSubview:scrollView3]; 130 131 [scrollView3 release]; 132 133 //第四个 134 135 UIScrollView *scrollView4 = [[UIScrollView alloc] initWithFrame:CGRectMake(860, 20, 240, 450)]; 136 137 scrollView4.contentSize = CGSizeMake(280, 0); 138 139 scrollView4.tag = 444; 140 141 UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)]; 142 143 image3.image = [UIImage imageNamed:@"4.JPG"]; 144 145 [scrollView4 addSubview:image3]; 146 147 [image3 release]; 148 149 scrollView4.delegate = self; 150 151 scrollView4.minimumZoomScale = 0.5; 152 153 scrollView4.maximumZoomScale = 2; 154 155 [scrollView addSubview:scrollView4]; 156 157 [scrollView4 release]; 158 159 //第五个 160 161 UIScrollView *scrollView5 = [[UIScrollView alloc] initWithFrame:CGRectMake(1140, 20, 240, 450)]; 162 163 scrollView5.contentSize = CGSizeMake(280, 0); 164 165 scrollView5.tag = 555; 166 167 UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 240, 400)]; 168 169 image4.image = [UIImage imageNamed:@"5.JPG"]; 170 171 [scrollView5 addSubview:image4]; 172 173 [image4 release]; 174 175 scrollView5.delegate = self; 176 177 scrollView5.minimumZoomScale = 0.5; 178 179 scrollView5.maximumZoomScale = 2; 180 181 [scrollView addSubview:scrollView5]; 182 183 [scrollView5 release]; 184 185 186 scrollView.delegate = self; 187 188 [self.view addSubview:scrollView]; 189 190 [scrollView release]; 191 192 193 194 UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(110, 400, 100,30)]; 195 196 pageControl.numberOfPages = 5; 197 198 pageControl.pageIndicatorTintColor = [UIColor blackColor]; 199 200 pageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; 201 202 [pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged]; 203 204 [self.view addSubview:pageControl]; 205 206 [pageControl release]; 207 208 209 210 self.page = pageControl; 211 212 213 214 215 216 } 217 218 - (void)pageControlAction:(UIPageControl *)pageControl 219 220 { 221 222 UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:1000]; 223 224 scrollView.contentOffset = CGPointMake(280 * self.page.currentPage, 0); 225 226 for (int i = 1; i < 6; i++) { 227 228 UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111 * i]; 229 230 scrollView.zoomScale = 1; 231 232 } 233 234 } 235 236 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 237 238 { 239 240 self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width; 241 242 } 243 244 245 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 246 247 { 248 249 return [scrollView.subviews firstObject]; 250 251 } 252 253 254 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 255 256 { 257 258 for (int i = 1; i < 6; i ++) { 259 260 UIScrollView *scrollView = (UIScrollView *) [self.view viewWithTag:111 * i]; 261 262 scrollView.zoomScale = 1; 263 264 } 265 266 } 267 268 269 - (void)didReceiveMemoryWarning 270 271 { 272 273 [super didReceiveMemoryWarning]; 274 275 // Dispose of any resources that can be recreated. 276 277 } 278 279 280 /* 281 282 #pragma mark - Navigation 283 284 285 // In a storyboard-based application, you will often want to do a little preparation before navigation 286 287 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 288 289 { 290 291 // Get the new view controller using [segue destinationViewController]. 292 293 // Pass the selected object to the new view controller. 294 295 } 296 297 */ 298 299 300 @end
以上是关于UI 运用滚动视图创建相册的方法的主要内容,如果未能解决你的问题,请参考以下文章