UI2_ScrollViewHomeWork

//
//  AppDelegate.m
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *root = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
    self.window.rootViewController  = nav;
    self.window.backgroundColor  = [UIColor whiteColor];
    //[self.window makeKeyAndVisible];
    
    return YES;
}




//
//  ViewController.h
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DetailViewController.h"

@interface ViewController : UIViewController <sendMessageReport>

@end

 

//
//  ViewController.m
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "CustomImageView.h"
#import "DetailViewController.h"

@interface ViewController ()
{
    UIScrollView *_scrollView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.automaticallyAdjustsScrollViewInsets = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    //_scrollView.backgroundColor = [UIColor cyanColor];
    NSInteger rowCount = 4;
    CGFloat space = 2;
    CGFloat imageViewWidth = (self.view.frame.size.width-2*5)/4;
    CGFloat imageViewHeight = imageViewWidth*1.5;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AlbumData" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    int i=0;
    for (NSDictionary *dict in array) {
        NSString *title = [dict objectForKey:@"TITLE"];
        NSString *name = [NSString stringWithFormat:@"%@.jpg",[dict objectForKey:@"NAME"]];
        UIImage *image = [UIImage imageNamed:name];
    
        CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(space+(space+imageViewWidth)*(i%rowCount), (i/rowCount)*(space+imageViewHeight)+space, imageViewWidth, imageViewHeight)];
        imageView.image = image;
        imageView.label.text = title;
        imageView.tag = 100+i;
        
        UITapGestureRecognizer  *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        
        [_scrollView addSubview:imageView];
        i++;
    }
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, (imageViewHeight+space)*8+space);
    [self.view addSubview:_scrollView];
}

- (void)tapGesture:(UITapGestureRecognizer *)tap
{
    DetailViewController *dvc = [[DetailViewController alloc] init];
    dvc.image = ((UIImageView *)tap.view).image;
    dvc.imageViewTag = tap.view.tag;
    dvc.delegate = self;
    
    [self.navigationController pushViewController:dvc animated:YES];
}

- (void)sendViewTag:(NSInteger)tag andTitle:(NSString *)title
{
    CustomImageView *imageView = (CustomImageView *)[_scrollView viewWithTag:tag];
    imageView.label.text = title;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//
//  CustomImageView.h
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomImageView : UIImageView

@property (nonatomic,strong) UILabel *label;
@property (nonatomic,copy)NSString *title;

@end


//
//  CustomImageView.m
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "CustomImageView.h"

@implementation CustomImageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)];
        self.label.alpha = 0.5;
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.adjustsFontSizeToFitWidth = YES;
        self.label.text = self.title;
        self.label.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.label];
    }
    return self;
}




@end

 

//
//  DetailViewController.h
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol sendMessageReport <NSObject>

- (void)sendViewTag:(NSInteger)tag andTitle:(NSString *)title;

@end


@interface DetailViewController : UIViewController

@property (nonatomic, strong)UIImage *image;
@property (nonatomic, assign)NSInteger imageViewTag;
@property (nonatomic, assign)id <sendMessageReport  > delegate;

@end



//
//  DetailViewController.m
//  UI2_ScrollViewHomeWork
//
//  Created by zhangxueming on 15/7/13.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 64, self.view.frame.size.width-20,50)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.tag = 200;
    [self.view addSubview:textField];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 64+50, self.view.frame.size.width-20, 300)];
    imageView.tag = 200;
    imageView.image = self.image;
    [self.view addSubview:imageView];
    
    UIBarButtonItem *item= [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(btnClick)];
    self.navigationItem.rightBarButtonItem = item;
}


- (void)btnClick
{
    UITextField *textField = (UITextField *)[self.view viewWithTag:200];
    
    if ([_delegate respondsToSelector:@selector(sendViewTag:andTitle:)]) {
        [_delegate sendViewTag:self.imageViewTag andTitle:textField.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

posted @ 2015-07-13 18:01  sirzhang  阅读(256)  评论(0编辑  收藏  举报
AmazingCounters.com