1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #import "TileViewController.h"
- #import "SkimViewController.h"
- #import "UIImageView+WebCache.h"
- @interface TileViewController ()
- {
- NSArray *_imageNames;
- UIScrollView *_scrollView;
- }
- @end
- @implementation TileViewController
- - (instancetype)initWithImageNames:(NSArray *)imageNames
- {
- if (self = [super init]) {
- _imageNames = [[NSArray alloc] initWithArray:imageNames];
-
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- //创建滚动视图
- [self createScrollView];
- //创建图片视图
- [self createImageViews];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)createScrollView
- {
- //取消对滚动视图内容的布局的影响
- if (@available(iOS 11.0, *)) {
- _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- CGSize size = self.view.frame.size;
- _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height-kNavOffSet-40-50)];
-
- [self.view addSubview:_scrollView];
- }
- - (void)createImageViews
- {
- CGSize size = _scrollView.frame.size;
- CGFloat gap = 3;
- CGFloat width = size.width/4-gap;
- CGFloat height = width;//*3/2
- for (NSInteger i=0; i<_imageNames.count; i++) {
- UIImageView *imageView = [[UIImageView alloc] init];
- [imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]] placeholderImage:[UIImage imageNamed:@"暂无图片"]];
- imageView.frame = CGRectMake(gap/2+i%4*(width+gap), gap+i/4*(height+gap), width, height);
-
- [_scrollView addSubview:imageView];
- _scrollView.contentSize = CGSizeMake(size.width, gap+(_imageNames.count+3)/4*(gap+height));
-
- //添加手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
- [imageView addGestureRecognizer:tap];
- //使用tag值记录图片数组中的下标
- imageView.tag = i;
- //打开交互
- imageView.userInteractionEnabled = YES;
- }
- }
- - (BOOL)prefersStatusBarHidden
- {
- return self.navigationController.navigationBarHidden;
- }
- - (void)tapHandle:(UITapGestureRecognizer *)tap
- {
- SkimViewController *svc = [[SkimViewController alloc] initWithImageNames:_imageNames index:tap.view.tag];
-
- [self.navigationController pushViewController:svc animated:NO];
-
- //告诉应用程序状态栏需要刷新(9.0以后不用管)
- [self setNeedsStatusBarAppearanceUpdate];
-
- }
- @end
|