5.5 UIScrollView 滚动视图 相册精装版

#import "AppDelegate.h"
#import "PhotosViewController.h"
#import "SharedViewController.h"
#import "AlbumsViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    
    //分栏控制器
    UITabBarController * tab = [[UITabBarController alloc] init];
    //图片视图控制器
    PhotosViewController * photosVC = [[PhotosViewController alloc] init];
    photosVC.title = @"Photos";
    UINavigationController * photosNav = [[UINavigationController alloc] initWithRootViewController:photosVC];
    //分享视图控制器
    SharedViewController * sharedVC = [[SharedViewController alloc] init];
    sharedVC.title = @"Shared";
    UINavigationController * sharedNav = [[UINavigationController alloc] initWithRootViewController:sharedVC];
    //相册视图控制器
    AlbumsViewController * albumsVC = [[AlbumsViewController alloc] init];
    albumsVC.title = @"Albums";
    UINavigationController * albumsNav = [[UINavigationController alloc] initWithRootViewController:albumsVC];
    
    tab.viewControllers = @[photosNav, sharedNav, albumsNav];
    
    //tabbar item的定制
    UITabBarItem * photosItem = [[UITabBarItem alloc] initWithTitle:@"Photos" image:[UIImage imageNamed:@"photos"] selectedImage:[UIImage imageNamed:@"photos_s"]];
    UITabBarItem * sharedItem = [[UITabBarItem alloc] initWithTitle:@"Shared" image:[UIImage imageNamed:@"shared"] selectedImage:[UIImage imageNamed:@"shared_s"]];
    UITabBarItem * albumsItem = [[UITabBarItem alloc] initWithTitle:@"Albums" image:[UIImage imageNamed:@"albums"] selectedImage:[UIImage imageNamed:@"albums_s"]];
    
    photosNav.tabBarItem = photosItem;
    sharedNav.tabBarItem = sharedItem;
    albumsNav.tabBarItem = albumsItem;
    
    _window.rootViewController = tab;
    
    return YES;
}

相册视图控制器

#import "AlbumsViewController.h"
#import "PhotosWallViewController.h"

@interface AlbumsViewController ()

@end

@implementation AlbumsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self makeNavigationItems];
    //添加功能按钮
    [self addButtons];
}

//添加功能按钮
- (void)addButtons {
    //1.拿到所有plist文件的名字
    NSArray * plistsPath = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:nil];
    ///Users/Naibin/Library/Developer/CoreSimulator/Devices/498C83D6-49D0-4004-A105-795F8C38BBDF/data/Containers/Bundle/Application/5FCA69E9-0603-45CF-A9BA-6D4350828A6C/Day9-Photos.app/Info.plist
    int count = 0;
    for (NSString * plistPath in plistsPath) {
        NSString * plistName = [[plistPath componentsSeparatedByString:@"/"] lastObject];
        if ([plistName hasPrefix:@"image_"] == YES) {
            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 80 + 64 * count, [[UIScreen mainScreen] bounds].size.width, 44)];
            //image_圣斗士.plist
            NSString * name = [plistName componentsSeparatedByString:@"."][0];
            name = [name substringFromIndex:6];
            //标题
            [button setTitle:name forState:UIControlStateNormal];
            //背景颜色
            [button setBackgroundColor:[UIColor blueColor]];
            //高亮状态下的标题颜色
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            //绑定事件
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:button];
            
            count++;
        }
    }
}

- (void)buttonClicked:(UIButton *)button {
    //下一级页面
    PhotosWallViewController * pwVC = [[PhotosWallViewController alloc] init];
    pwVC.plistName = button.titleLabel.text;
    //推入后隐藏tabbar
    pwVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:pwVC animated:YES];
}

- (void)makeNavigationItems {
    UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
    UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
    //设置导航栏左右按钮
    self.navigationItem.leftBarButtonItem = item1;
    self.navigationItem.rightBarButtonItems = @[item3, item2];
    //应用的本地/国际化
}
@end

照片墙视图控制器

#import <UIKit/UIKit.h>

@interface PhotosWallViewController : UIViewController

//plist文件的名字
@property (nonatomic, copy) NSString * plistName;

@end
#import "PhotosWallViewController.h"
#import "ADImageView.h"
#import "PhotoShowViewController.h"

#define SCREEN_SIZE [[UIScreen mainScreen] bounds].size

@interface PhotosWallViewController ()

@end

@implementation PhotosWallViewController {
    UIScrollView * _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //默认为YES
    //为滚动视图自动留出64像素的内边距
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建滚动视图
    [self createScrollView];
    //添加子视图
    [self addSubViews];
}

- (void)createScrollView {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:_scrollView];
}

- (void)addSubViews {
    /*
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    label.backgroundColor = [UIColor orangeColor];
    [_scrollView addSubview:label];
     */
    
    //名字
    NSString * plistName = [NSString stringWithFormat:@"image_%@", _plistName];
    //路径
    NSString * plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSArray * arr = [NSArray arrayWithContentsOfFile:plistPath];
    //创建子视图
    for (int i = 0; i < arr.count; i++) {
        ADImageView * imageView = [[ADImageView alloc] initWithFrame:CGRectMake((i % 4) * SCREEN_SIZE.width / 4, (i / 4) * SCREEN_SIZE.width / 4, SCREEN_SIZE.width / 4, SCREEN_SIZE.width / 4)];
        //获取图片
        NSString * imageName = arr[i][@"imageName"];
        NSArray * nameAndType = [imageName componentsSeparatedByString:@"."];
        NSString * imagePath = [[NSBundle mainBundle] pathForResource:nameAndType[0] ofType:[nameAndType lastObject]];
        UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
        
        //设置ImageView
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        imageView.image = image;
        
        //给imageView添加事件
        [imageView addTarget:self withAction:@selector(imageTapped:)];
        imageView.tag = 100 + i;
        
        [_scrollView addSubview:imageView];
    }
    
    // 1 2 3 4 5 6 7 8 9 10
    // 1 1 1 1 2 2 2 2 3 3
    
    //设置contentSize
    _scrollView.contentSize = CGSizeMake(SCREEN_SIZE.width, ((arr.count - 1) / 4 + 1) * SCREEN_SIZE.width / 4);
}

- (void)imageTapped:(ADImageView *)imageView {
    PhotoShowViewController * showVC = [[PhotoShowViewController alloc] init];
    showVC.plistName = _plistName;
    showVC.currentPage = imageView.tag - 100;
    
    [self.navigationController pushViewController:showVC animated:YES];
}
@end

相册展示控制器

#import <UIKit/UIKit.h>

@interface PhotoShowViewController : UIViewController

//plist名字
@property (nonatomic, copy) NSString * plistName;
//初始化显示第index张
@property (nonatomic, assign) NSUInteger currentPage;

@end

#import "PhotoShowViewController.h"
#import "ADImageView.h"

#define SCREEN_SIZE [[UIScreen mainScreen] bounds].size

@interface PhotoShowViewController () <UIScrollViewDelegate>

@end

@implementation PhotoShowViewController {
    UIScrollView * _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建滚动视图
    [self createScrollView];
    //创建子视图
    [self addSubViews];
}

- (void)createScrollView {
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_scrollView];
}

- (void)addSubViews {
    //添加子视图
    NSString * plistName = [NSString stringWithFormat:@"image_%@", _plistName];
    NSString * plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSArray * arr = [NSArray arrayWithContentsOfFile:plistPath];
    //遍历数组
    for (int i = 0; i < arr.count; i++) {
        ADImageView * imageView = [[ADImageView alloc] initWithFrame:CGRectMake(i * SCREEN_SIZE.width, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
        //获取图片
        NSString * imageName = arr[i][@"imageName"];
        NSArray * nameAndType = [imageName componentsSeparatedByString:@"."];
        NSString * imagePath = [[NSBundle mainBundle] pathForResource:nameAndType[0] ofType:[nameAndType lastObject]];
        UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
        
        imageView.image = image;
        
        //修改图片显示模式
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        //绑定事件
        [imageView addTarget:self withAction:@selector(imageTapped:)];
        
        //添加子视图
        [_scrollView addSubview:imageView];
    }
    //设置ContentSize
    _scrollView.contentSize = CGSizeMake(arr.count * SCREEN_SIZE.width, SCREEN_SIZE.height);
    
    //修改ContentOffset
    _scrollView.contentOffset = CGPointMake(_currentPage * SCREEN_SIZE.width, 0);
    
    //初始化标题
    self.title = [NSString stringWithFormat:@"%ld / %ld", (_currentPage + 1), arr.count];
}

- (void)imageTapped:(ADImageView *)imageView {
    if (self.navigationController.navigationBar.isHidden == NO) {
        //隐藏
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        //和系统相关的设定,只有UIApplication对象有权利修改
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
        
        self.view.backgroundColor = [UIColor blackColor];
    }else {
        //显示
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger titlePage = (scrollView.contentOffset.x / SCREEN_SIZE.width + 1);
    self.title = [NSString stringWithFormat:@"%ld / %ld", titlePage,  (NSInteger)(scrollView.contentSize.width / SCREEN_SIZE.width)];
}
@end

图片点击处理


#import <UIKit/UIKit.h>

@interface ADImageView : UIImageView

//给图片添加点击事件
- (void)addTarget:(id)target withAction:(SEL)action;

@end
#import "ADImageView.h"

@implementation ADImageView {
    id _target;
    SEL _action;
}

- (void)addTarget:(id)target withAction:(SEL)action {
    self.userInteractionEnabled = YES;
    
    _target = target;
    _action = action;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([_target respondsToSelector:_action]) {
        //如果_target能够响应_action,则发送该消息
        [_target performSelector:_action withObject:self];
    }
}

@end

案列展示图

案列展示图1.png
案列展示图2.png
案列展示图3.png
案列展示图4.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容