IOS_UI_day5_UIScrollView+代理设计模式+键盘

H:/IOS_UI/day5-01-UIScrollView-大图片展示-MJViewController.h
//
//  MJViewController.h
//  01-UIScrollView01-大图片展示
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
- (IBAction)down:(UIButton *)sender;

@end

H:/IOS_UI/day5-01-UIScrollView-大图片展示-MJViewController.m
//
//  MJViewController.m
//  01-UIScrollView01-大图片展示
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"
@interface MJViewController ()
{
	//用成员变量记住,因为后面,手动点击按钮的时候,要改变_scrollView的contentOffset
    UIScrollView *_scrollView;
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
	//设置UIScrollView的可视范围:frame中的size
    scrollView.frame = CGRectMake(0, 0, 250, 250); 
	//背景颜色-灰
    scrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:scrollView];
	
    // 2.创建UIImageView(高清大图片)
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"big.jpg"];
    CGFloat imgW = imageView.image.size.width; // 图片的宽度
    CGFloat imgH = imageView.image.size.height; // 图片的高度
    imageView.frame = CGRectMake(0, 0, imgW, imgH);
    [scrollView addSubview:imageView];
	
    // 3.设置scrollView的属性
    //关键! 设置UIScrollView的滚动范围(内容大小)等价于高清大图尺寸
    scrollView.contentSize = imageView.image.size;
    // 不显示水平/垂直滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    // 用来记录scrollview滚动的位置
//    scrollView.contentOffset = ;
    // 去掉弹簧效果,不好看
//    scrollView.bounces = NO;
    // 增加额外的滚动区域,不知道到底有什么用处
    // top  left  bottom  right
    scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
    _scrollView = scrollView;
}
- (IBAction)down:(UIButton *)sender {
    [UIView animateWithDuration:1.0 animations:^{
//        CGPoint offset = _scrollView.contentOffset;
//        offset.y += 150;
//        _scrollView.contentOffset = offset;
        
        _scrollView.contentOffset = CGPointMake(0, 0);
    }];
}
@end

H:/IOS_UI/day5-02-UIScrollView-大图片展示-MJViewController.h
//
//  MJViewController.h
//  02-UIScrollView02-图片缩放
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController

@end

H:/IOS_UI/day5-02-UIScrollView-大图片展示-MJViewController.m
//
//  MJViewController.m
//  02-UIScrollView02-图片缩放
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJViewController.h"
@interface MJViewController () <UIScrollViewDelegate>
{
    UIImageView *_imageView;
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.添加UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
//    scrollView.frame = CGRectMake(0, 0, 320, 460);
	//设置scrollView的镜头大小
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    // 2.添加图片
    UIImage *image = [UIImage imageNamed:@"big.jpg"];
    // 调用initWithImage:方法创建出来的UIImageView,它的宽高默认跟图片的宽高一样
    _imageView = [[UIImageView alloc] initWithImage:image];
//    // 设置图片
//    imageView.image = [UIImage imageNamed:@"big.jpg"];
//    // 设置frame
//    imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
    [scrollView addSubview:_imageView];
    //设置UIScrollView的滚动范围(内容大小)等价于高清大图尺寸
    scrollView.contentSize = image.size;
    //重点! 设置scrollview的代理对象(代理设计模式)控制器成为代理
    scrollView.delegate = self;
    // 设置最大伸缩比例
    scrollView.maximumZoomScale = 2.0;
    // 设置最小伸缩比例
    scrollView.minimumZoomScale = 0.2;
}
#pragma mark - 成为代理对象的UIScrollView必须实现的 代理方法
#pragma mark 这个方法返回的控件就能进行捏合手势缩放操作
#pragma mark 当UIScrollView尝试进行缩放的时候就会调用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
	//这个是重点!即告诉ScrollView对图片进行缩放
    return _imageView;
}
#pragma mark 当缩放完毕的时候调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
//    NSLog(@"结束缩放 - %f", scale);
}
#pragma mark 当正在缩放的时候调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//    NSLog(@"-----");
}
@end

H:/IOS_UI/day5-03-代理设计模式-Baby.h
//
//  Baby.h
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "BabyBaomuXieyi.h"

@interface Baby : NSObject
@property (nonatomic, strong) id<BabyBaomuXieyi> baomu; // 任何人都能做保姆

@property (nonatomic, assign) int age; // 年龄

- (void)hungry; // 饿了
- (void)cry; // 哭了
- (void)sleep; // 想睡觉
@end

H:/IOS_UI/day5-03-代理设计模式-Baby.m
//
//  Baby.m
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Baby.h"

@implementation Baby

- (void)cry
{
    NSLog(@"%d岁的baby哭了", _age);
    
    // 通知保姆(护士)
    [_baomu babyCry:self];
}

- (void)sleep
{
    NSLog(@"%d岁的baby想睡觉", _age);
    
    // 通知保姆
    [_baomu babySleep:self];
}

- (void)hungry
{
    NSLog(@"%d岁的baby想吃东西", _age);
    
    // 通知保姆
    [_baomu babyHungry:self];
}

@end

H:/IOS_UI/day5-03-代理设计模式-BabyBaomuXieyi.h
//
//  BabyBaomuXieyi.h
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
@class Baby;

@protocol BabyBaomuXieyi <NSObject>
- (void)babyCry:(Baby *)baby;
- (void)babySleep:(Baby *)baby;
- (void)babyHungry:(Baby *)baby;
@end

H:/IOS_UI/day5-03-代理设计模式-Dog.h
//
//  Dog.h
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BabyBaomuXieyi.h"
@interface Dog : NSObject <BabyBaomuXieyi>

@end

H:/IOS_UI/day5-03-代理设计模式-Dog.m
//
//  Dog.m
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Dog.h"
#import "Baby.h"

@implementation Dog
- (void)babyCry:(Baby *)baby
{
    NSLog(@"狗正在安慰%d岁的baby", baby.age);
}

- (void)babyHungry:(Baby *)baby
{
    NSLog(@"狗喂%d岁的baby吃东西", baby.age);
}

- (void)babySleep:(Baby *)baby
{
    NSLog(@"狗哄%d岁的baby去睡觉", baby.age);
}
@end

H:/IOS_UI/day5-03-代理设计模式-Nurse.h
//
//  Nurse.h
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
@class Baby;

@interface Nurse : NSObject
- (void)babyCry:(Baby *)baby;
- (void)babySleep:(Baby *)baby;
- (void)babyHungry:(Baby *)baby;
@end

H:/IOS_UI/day5-03-代理设计模式-Nurse.m
//
//  Nurse.m
//  03-代理设计模式
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Nurse.h"
#import "Baby.h"

@implementation Nurse

- (void)babyCry:(Baby *)baby
{
    NSLog(@"护士正在安慰%d岁的baby", baby.age);
}

- (void)babyHungry:(Baby *)baby
{
    NSLog(@"护士喂%d岁的baby吃东西", baby.age);
}

- (void)babySleep:(Baby *)baby
{
    NSLog(@"护士哄%d岁的baby去睡觉", baby.age);
}

@end

H:/IOS_UI/day5-04-UIScrollView分页-MJViewController.h
//
//  MJViewController.h
//  04-UIScrollView03-分页
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

H:/IOS_UI/day5-04-UIScrollView分页-MJViewController.m
//
//  MJViewController.m
//  04-UIScrollView03-分页
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJViewController.h"
#define kCount 8
@interface MJViewController () <UIScrollViewDelegate>
{
	//成员变量:....
    UIPageControl *_pageControl;
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;
    for (int i = 0; i< kCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        // 1.根据i设置每张图片的frame,y为0,x,一张接一张口口口口口口口口
        imageView.frame = CGRectMake(i * w, 0, w, h);
        // 2.通过图片名,设置图片
        NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + 1];
        imageView.image = [UIImage imageNamed:imgName];
        [_scrollView addSubview:imageView];
    }
    //contentSize为滚动范围,height == 0 代表 禁止垂直方向滚动
    _scrollView.contentSize = CGSizeMake(kCount * w, 0);
    _scrollView.showsHorizontalScrollIndicator = NO;
	//关键!!!,它会以srollView的frame尺寸进行分页
    _scrollView.pagingEnabled = YES;
	
	//它控制器成为srollView的代理,目的是监听滚动,控制pageControl的圆点
    _scrollView.delegate = self;
	
    //======================= 添加PageControl...//=======================
    UIPageControl *pageControl = [[UIPageControl alloc] init];
	//...的中心位于底部,先用center设置x,y,再用bounds设置w h
    pageControl.center = CGPointMake(w * 0.5, h - 20);
    pageControl.bounds = CGRectMake(0, 0, 150, 50);
	//设置总页数,一共显示多少个圆点(多少页)
    pageControl.numberOfPages = kCount;  
    // 设置指示页的圆点颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 设置选中页的圆点颜色
    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    // 禁止默认的点击功能
    pageControl.enabled = NO;
    [self.view addSubview:pageControl];
	
	
    _pageControl = pageControl;
}

#pragma mark - UIScrollView的代理方法
#pragma mark 当scrollView正在滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{	//偏移x的值0~320~640....处以320就是第N张图片在当前
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
//    NSLog(@"%d", page);
    // 设置页码
    _pageControl.currentPage = page;
}
@end

H:/IOS_UI/day5-05-键盘的简单处理-MJViewController.h
//
//  MJViewController.h
//  05-键盘的简单处理
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
- (IBAction)exitkeyboard;
@property (weak, nonatomic) IBOutlet UITextField *nameFeild;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;

@end

H:/IOS_UI/day5-05-键盘的简单处理-MJViewController.m
//
//  MJViewController.m
//  05-键盘的简单处理
//
//  Created by apple on 13-11-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.加载xib创建工具条
    UIToolbar *keyboardTool = [[NSBundle mainBundle] 
				loadNibNamed:@"KeyboardTool" owner:nil options:nil][0];
    // 设置键盘顶部显示的工具条
    _nameFeild.inputAccessoryView = keyboardTool;
    _phoneField.inputAccessoryView = keyboardTool;
    _addressField.inputAccessoryView = keyboardTool;
	//变成第一响应者(就能召唤出键盘)
//    [_nameFeild becomeFirstResponder];  
//    _nameFeild.inputView // 自定义键盘
}
#pragma mark 退出键盘
- (IBAction)exitkeyboard {
    // 响应者 : 能处理事件的对象
    // 第一响应者 : 叫出键盘的那个文本框控件
    // 退出键盘的原理 : 某个文本框控件 不想 当第一响应者
    // 退出键盘
//    [_phoneField resignFirstResponder];
//    [_nameFeild resignFirstResponder];
//    [_addressField resignFirstResponder];
    // self.view内部所有文本框都会退出键盘
    [self.view endEditing:YES];
}
@end

你可能感兴趣的:(ios,UI,uiscrollview)