TileViewController.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #import "TileViewController.h"
  2. #import "SkimViewController.h"
  3. @interface TileViewController ()
  4. {
  5. NSArray *_imageNames;
  6. UIScrollView *_scrollView;
  7. }
  8. @end
  9. @implementation TileViewController
  10. - (instancetype)initWithImageNames:(NSArray *)imageNames
  11. {
  12. if (self = [super init]) {
  13. _imageNames = [[NSArray alloc] initWithArray:imageNames];
  14. }
  15. return self;
  16. }
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. //创建滚动视图
  20. [self createScrollView];
  21. //创建图片视图
  22. [self createImageViews];
  23. }
  24. - (void)didReceiveMemoryWarning {
  25. [super didReceiveMemoryWarning];
  26. // Dispose of any resources that can be recreated.
  27. }
  28. - (void)createScrollView
  29. {
  30. //取消对滚动视图内容的布局的影响
  31. if (@available(iOS 11.0, *)) {
  32. _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  33. }else {
  34. self.automaticallyAdjustsScrollViewInsets = NO;
  35. }
  36. CGSize size = self.view.frame.size;
  37. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height-kNavOffSet-40-50-SafeAreaBottomHeight)];
  38. [self.view addSubview:_scrollView];
  39. }
  40. - (void)createImageViews
  41. {
  42. CGSize size = _scrollView.frame.size;
  43. CGFloat gap = 1;
  44. CGFloat width = size.width/4-gap;
  45. CGFloat height = width;//width*3/2
  46. for (NSInteger i=0; i<_imageNames.count; i++) {
  47. UIImageView *imageView = [[UIImageView alloc] init];
  48. [imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]] placeholderImage:[UIImage imageNamed:@"NOImg"]];
  49. imageView.frame = CGRectMake(gap/2+i%4*(width+gap), gap+i/4*(height+gap), width, height);
  50. [_scrollView addSubview:imageView];
  51. _scrollView.contentSize = CGSizeMake(size.width, gap+(_imageNames.count+3)/4*(gap+height));
  52. //添加手势
  53. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
  54. [imageView addGestureRecognizer:tap];
  55. //使用tag值记录图片数组中的下标
  56. imageView.tag = i;
  57. //打开交互
  58. imageView.userInteractionEnabled = YES;
  59. }
  60. }
  61. - (BOOL)prefersStatusBarHidden
  62. {
  63. return self.navigationController.navigationBarHidden;
  64. }
  65. - (void)tapHandle:(UITapGestureRecognizer *)tap
  66. {
  67. SkimViewController *svc = [[SkimViewController alloc] initWithImageNames:_imageNames index:tap.view.tag];
  68. [self.navigationController pushViewController:svc animated:NO];
  69. //告诉应用程序状态栏需要刷新(9.0以后不用管)
  70. [self setNeedsStatusBarAppearanceUpdate];
  71. }
  72. @end