美文网首页
5.5 UIScrollView 滚动视图 相册精装版

5.5 UIScrollView 滚动视图 相册精装版

作者: 草根小强 | 来源:发表于2019-04-16 11:49 被阅读0次
#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

相关文章

网友评论

      本文标题:5.5 UIScrollView 滚动视图 相册精装版

      本文链接:https://www.haomeiwen.com/subject/yqovwqtx.html