1、纯代码实现步骤如下:
(1)第一步:如果新建一个空的工程,需要先新建一个控制器类假设为ViewController,然后在主程序代理类的实现文件AppDelegate.m的第一个方法即- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中新建一个控制器类的对象,并将当前窗口绑定此控制器对象,代码如下:
ViewController *myVC=[[ViewControl alloc] init];
self.window.rootViewController=myVC;
(2)第二步:在控制器的.h文件中设置组件属性和监听方法;如果要执行某些组件例如文本输入框UITextField或滚动试图UIScrollView的回调方法,还要让当前控制器的类遵守每个组件的协议例如:UITextFieldDelegate和UIScrllViewDelegate
(3)第三步:在控制器类的实现文件.m文件中的viewDidLoad方法中对已经声明的组件对象进行分配空间初始化并设置各个组件在各个状态下的各种属性。注意:如果想在使用某些组件的过程中触发某些操作,例如拖动UIScrollView内部的内容时或在点击文本输入框成为第一响应者时将触发哪些操作。就要将已经遵守的对应协议中的相应方法拿出来对其进行编辑实现。此外还要将执行回调方法的组件把自己设为自身的代理,即代码:…….delegate=self;
(4)第四步:为组件绑定监听方法,一般都是为按钮之类的绑定监听方法,例如代码:…….addTarget..... 切记:当有多个按钮时,只设置一个监听方法即可,但是此监听方法必须要有一个UIButton类型的对象作为形参且绑定此方法的多个按钮都必须设置不同的Tag值,以此传入形参的Tag值来判断是哪个按钮。并进行不同的处理。
2、设置操作控件时执行回调方法的步骤:
对控件执行某些操作的过程中,如果想让此控件在执行操作的每个阶段分别触发那些回调方法必须设置以下步骤:
(1)让当前控件所在的控制器遵守此控件的协议。
(2)在设置控件属性时把自身代理设置为自身。
(3)添加此控件协议中需要的回调方法,并进行内容的实现。
注意:当有多个相同类型的控件都要执行回调方法时,就必须分别为这些多个要执行回调方法的相同类型的控件分别设置不同的tag值,在添加的此种控件协议的方法中就可以根据传入的Tag值进行区分。
3、如何方便的使用屏幕的宽度和高度:
获取屏幕的宽高,并把屏幕的宽高使用预编译。格式如下:
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
4、如何从当前控制器跳到别的控制器所控制的View中:
在某按钮的监听方法中调用如下方法即可——>
[self presentViewController:myVC2 animated:YES completion:^{
}];
5、当有多个按钮时,添加监听方法的步骤:
(1)在控制器的声明文件.h中声明监听方法,必须要有UIButton *类型的参数以便根据传过来参数中的tag值进行区分触发的是哪个按钮,例如:
-(void)log:(UIButton *)sender;
(2)为各个按钮在设置属性后绑定此监听方法,调用addTarget前缀的方法。例如:[_cancel addTarget:self action:@selector(log:) forControlEvents:UIControlEventTouchUpInside];
(3)为每个绑定此监听方法的按钮分别设置不同的tag值以作为区分标致,例如: [_cancel setTag:200];
6、如何在点击按钮时,使第一响应者辞职:
就是在点击按钮时,将输入框调出的键盘收回
方法一:
如下,在按钮的监听方法处理的事件中调用各个文本输入框的辞职第一响应者的方法即resignFirstResponder方法,例如:
[_user resignFirstResponder];
[_passWord resignFirstResponder];
方法二:
直接在在监听方法内将以上代码去掉,直接将当前所在控制器所在的View停止编辑即可,即调用View的endingEditing方法,例如:
[self.view endEditing:YES];
7、如何在点击屏幕别处时也能辞职第一响应者,也就是收回键盘:
(1)在控制器的声明文件.h文件中添加方法touchsBegan前缀的方法:即添加—>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(2)在.m文件中实现此文件,即在此方法中辞职所有的第一响应者,或让输入控件所在的view停止编辑。具体如下:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [_user resignFirstResponder];
// [_passWord resignFirstResponder];
[self.view endEditing:YES];
}
注意:让控制器的view停止编辑意味着所有此view的所有能进行编辑调出键盘的子控件都将辞职第一响应者。
8、UIScrollView手动代码设置步骤:
(1)让UIScrollView所在的控制器类遵守UIScrollViewDelegate即此滚动组件的代理协议,然后直接就可以在控制器的实现文件中实现协议中的任意回调方法了。
(2)在控制器的.h文件中声明UIScrollView控件的变量即:
@property (nonatomic, strong) UIScrollView *scroll;
(3)在控制器.m文件中的viewDidLoad方法中为此对象分配空间并初始化。具体如下:
// UIScrollView
CGRect rect1=CGRectMake(0, 0, width, height);
_scroll=[[UIScrollView alloc] initWithFrame:rect1];
(4)对此滚动试图添加内容即添加子控件用addSubview方法,如
for (int i=0; i<4; i++) {
UIImageView *imav=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, height)];
imav.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]];
[_scroll addSubview:imav];
}
(5)必须将要滚动所有内容的总大小告诉UIScrollView,即将内容的总大小赋给此UIScrollView对象的contentSize。如:
_scroll.contentSize=CGSizeMake(width*4, height);
(6)对此UIScrollView对象进行属性设置,具体如下:
_scroll.pagingEnabled=YES; // 设置可以翻页
_scroll.bounces=NO; // 设置不带弹性
_scroll.showsHorizontalScrollIndicator=NO;// 取消显示水平滚动条
_scroll.showsVerticalScrollIndicator=NO;// 取消显示垂直滚动条
(7)将此UIScrollView的代理设置为自身,如:
_scroll.delegate=self;
(8)为此UIScrollView对象添加回调方法。例如:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[_label setText:@"开始移动"];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int i=floor(scrollView.contentOffset.x/width)+1;
[_label setText:[NSString stringWithFormat:@"第 %d 页",i]];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_label setText:@"正在移动"];
编辑viewController.h如下:
编辑viewController2.h如下:
编辑viewController3.m如下:
运行结果如下:
(1)第一步:如果新建一个空的工程,需要先新建一个控制器类假设为ViewController,然后在主程序代理类的实现文件AppDelegate.m的第一个方法即- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中新建一个控制器类的对象,并将当前窗口绑定此控制器对象,代码如下:
ViewController *myVC=[[ViewControl alloc] init];
self.window.rootViewController=myVC;
(2)第二步:在控制器的.h文件中设置组件属性和监听方法;如果要执行某些组件例如文本输入框UITextField或滚动试图UIScrollView的回调方法,还要让当前控制器的类遵守每个组件的协议例如:UITextFieldDelegate和UIScrllViewDelegate
(3)第三步:在控制器类的实现文件.m文件中的viewDidLoad方法中对已经声明的组件对象进行分配空间初始化并设置各个组件在各个状态下的各种属性。注意:如果想在使用某些组件的过程中触发某些操作,例如拖动UIScrollView内部的内容时或在点击文本输入框成为第一响应者时将触发哪些操作。就要将已经遵守的对应协议中的相应方法拿出来对其进行编辑实现。此外还要将执行回调方法的组件把自己设为自身的代理,即代码:…….delegate=self;
(4)第四步:为组件绑定监听方法,一般都是为按钮之类的绑定监听方法,例如代码:…….addTarget..... 切记:当有多个按钮时,只设置一个监听方法即可,但是此监听方法必须要有一个UIButton类型的对象作为形参且绑定此方法的多个按钮都必须设置不同的Tag值,以此传入形参的Tag值来判断是哪个按钮。并进行不同的处理。
2、设置操作控件时执行回调方法的步骤:
对控件执行某些操作的过程中,如果想让此控件在执行操作的每个阶段分别触发那些回调方法必须设置以下步骤:
(1)让当前控件所在的控制器遵守此控件的协议。
(2)在设置控件属性时把自身代理设置为自身。
(3)添加此控件协议中需要的回调方法,并进行内容的实现。
注意:当有多个相同类型的控件都要执行回调方法时,就必须分别为这些多个要执行回调方法的相同类型的控件分别设置不同的tag值,在添加的此种控件协议的方法中就可以根据传入的Tag值进行区分。
3、如何方便的使用屏幕的宽度和高度:
获取屏幕的宽高,并把屏幕的宽高使用预编译。格式如下:
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
4、如何从当前控制器跳到别的控制器所控制的View中:
在某按钮的监听方法中调用如下方法即可——>
[self presentViewController:myVC2 animated:YES completion:^{
}];
5、当有多个按钮时,添加监听方法的步骤:
(1)在控制器的声明文件.h中声明监听方法,必须要有UIButton *类型的参数以便根据传过来参数中的tag值进行区分触发的是哪个按钮,例如:
-(void)log:(UIButton *)sender;
(2)为各个按钮在设置属性后绑定此监听方法,调用addTarget前缀的方法。例如:[_cancel addTarget:self action:@selector(log:) forControlEvents:UIControlEventTouchUpInside];
(3)为每个绑定此监听方法的按钮分别设置不同的tag值以作为区分标致,例如: [_cancel setTag:200];
6、如何在点击按钮时,使第一响应者辞职:
就是在点击按钮时,将输入框调出的键盘收回
方法一:
如下,在按钮的监听方法处理的事件中调用各个文本输入框的辞职第一响应者的方法即resignFirstResponder方法,例如:
[_user resignFirstResponder];
[_passWord resignFirstResponder];
方法二:
直接在在监听方法内将以上代码去掉,直接将当前所在控制器所在的View停止编辑即可,即调用View的endingEditing方法,例如:
[self.view endEditing:YES];
7、如何在点击屏幕别处时也能辞职第一响应者,也就是收回键盘:
(1)在控制器的声明文件.h文件中添加方法touchsBegan前缀的方法:即添加—>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(2)在.m文件中实现此文件,即在此方法中辞职所有的第一响应者,或让输入控件所在的view停止编辑。具体如下:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [_user resignFirstResponder];
// [_passWord resignFirstResponder];
[self.view endEditing:YES];
}
注意:让控制器的view停止编辑意味着所有此view的所有能进行编辑调出键盘的子控件都将辞职第一响应者。
8、UIScrollView手动代码设置步骤:
(1)让UIScrollView所在的控制器类遵守UIScrollViewDelegate即此滚动组件的代理协议,然后直接就可以在控制器的实现文件中实现协议中的任意回调方法了。
(2)在控制器的.h文件中声明UIScrollView控件的变量即:
@property (nonatomic, strong) UIScrollView *scroll;
(3)在控制器.m文件中的viewDidLoad方法中为此对象分配空间并初始化。具体如下:
// UIScrollView
CGRect rect1=CGRectMake(0, 0, width, height);
_scroll=[[UIScrollView alloc] initWithFrame:rect1];
(4)对此滚动试图添加内容即添加子控件用addSubview方法,如
for (int i=0; i<4; i++) {
UIImageView *imav=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, height)];
imav.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]];
[_scroll addSubview:imav];
}
(5)必须将要滚动所有内容的总大小告诉UIScrollView,即将内容的总大小赋给此UIScrollView对象的contentSize。如:
_scroll.contentSize=CGSizeMake(width*4, height);
(6)对此UIScrollView对象进行属性设置,具体如下:
_scroll.pagingEnabled=YES; // 设置可以翻页
_scroll.bounces=NO; // 设置不带弹性
_scroll.showsHorizontalScrollIndicator=NO;// 取消显示水平滚动条
_scroll.showsVerticalScrollIndicator=NO;// 取消显示垂直滚动条
(7)将此UIScrollView的代理设置为自身,如:
_scroll.delegate=self;
(8)为此UIScrollView对象添加回调方法。例如:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[_label setText:@"开始移动"];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int i=floor(scrollView.contentOffset.x/width)+1;
[_label setText:[NSString stringWithFormat:@"第 %d 页",i]];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_label setText:@"正在移动"];
}
代码验证示例如下:
第一步:新建一个空的工程
第二步:新建第一个控制器ViewController
在主函数代理类AppDelegate.m中进行控制器的设置:
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor grayColor];
ViewController *myVC=[[ViewController alloc] init];
self.window.rootViewController=myVC;
[self.window makeKeyAndVisible];
return YES;
}
、、、、、、
编辑viewController.h如下:
//
// ViewController.h
// denglu
//
// Created by apple on 15/8/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *logo;
@property (nonatomic, strong) UILabel *error;
@property (nonatomic, strong) UITextField *user;
@property (nonatomic, strong) UIButton *cancel;
@property (nonatomic, strong) UITextField *passWord;
@property (nonatomic, strong) UIButton *login;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIImageView *xhx1;
@property (nonatomic, strong) UIImageView *xhx2;
@property (nonatomic, assign) BOOL allowUser;
@property (nonatomic, assign) BOOL allowPass;
-(void)log:(UIButton *)sender;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
编辑viewController.m如下:
//
// ViewController.m
// denglu
//
// Created by apple on 15/8/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.allowUser=NO;
self.allowPass=NO;
// 设置当前view的背景
[self.view setBackgroundColor:[UIColor darkGrayColor]];
// 设置logo代码
{
CGRect logoRect=CGRectMake(0, 0, 143, 48); // 创建logo的位置和大小
// 为UIImageView类型的对象_logo开辟空间并同时设置图片
self.logo=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_首页Logo.png"]];
self.logo.contentMode=UIViewContentModeScaleAspectFit; // 设置内容填充模式
_logo.frame = logoRect; //设置控件的大小和相对位置(相对于父控件的位置)
CGPoint originCenter=self.logo.center; // 获取组件中心点的坐标
originCenter.x=screenWidth/2; // 修改坐标
originCenter.y=screenHeight/7;
self.logo.center=originCenter; // 重新赋值
[self.logo setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:_logo];
}
// 设置用户名标签
{
CGRect label1Rect = CGRectMake(screenWidth/7, screenHeight*0.4, 70,30);
self.label1=[[UILabel alloc] initWithFrame:label1Rect];
[self.label1 setBackgroundColor:[UIColor clearColor]];
[self.label1 setText:@"用户名"];
self.label1.contentMode=UIViewContentModeScaleAspectFit;
[_label1 setTextColor:[UIColor whiteColor]];
[self.view addSubview:_label1];
}
// 设置用户名输入框
{
CGRect lab1= _label1.frame;
double x=lab1.origin.x+lab1.size.width;
double y=lab1.origin.y;
double w=screenWidth-2*lab1.origin.x-lab1.size.width;
double h=lab1.size.height;
CGRect userRect=CGRectMake(x, y, w, h);
_user=[[UITextField alloc] initWithFrame:userRect];
[_user setBackgroundColor:[UIColor whiteColor]];
self.user.delegate = self;
self.user.tag=400; // 因为为两个输入控件设置了各自的代理,公用同一个回调方法
[self.view addSubview:_user];
}
// 在输入框下面设置下划线
{
CGRect originRect = _xhx1.frame;
CGRect label1 = _label1.frame;
_xhx1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_首页输入框底线@2x.png"]];
originRect.origin.x=label1.origin.x;
originRect.origin.y=label1.origin.y+label1.size.height+2;
originRect.size.width=screenWidth-2*label1.origin.x;
originRect.size.height=1.5;
_xhx1.frame=originRect;
[self.view addSubview:_xhx1];
}
// 设置用户密码标签
{
CGRect xhxRect1= _xhx1.frame;
CGRect originRect;
originRect.origin.x=xhxRect1.origin.x;
originRect.origin.y=xhxRect1.origin.y+xhxRect1.size.height+2;
originRect.size.height=_label1.frame.size.height;
originRect.size.width=_label1.frame.size.width;
_label2=[[UILabel alloc] initWithFrame:originRect];
[_label2 setText:@"密码"];
[_label2 setTextColor:[UIColor whiteColor]];
_label2.contentMode=UIViewContentModeScaleAspectFit;
[self.view addSubview:_label2];
}
// 设置用户密码输入框
{
CGRect rect;
rect.origin.x=_user.frame.origin.x;
rect.origin.y=_label2.frame.origin.y;
rect.size.height=_user.frame.size.height;
rect.size.width=_user.frame.size.width;
_passWord=[[UITextField alloc] initWithFrame:rect];
[_passWord setBackgroundColor:[UIColor whiteColor]];
_passWord.secureTextEntry=YES;
_passWord.delegate=self;
_passWord.tag=500;
[self.view addSubview:_passWord];
}
// 设置下划线
{
CGRect originRect;
CGRect label2=_label2.frame;
_xhx2=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_首页输入框底线@2x.png"]];
originRect.origin.x=label2.origin.x;
originRect.origin.y=label2.origin.y+label2.size.height+2;
originRect.size.width=screenWidth-2*label2.origin.x;
originRect.size.height=1.5;
_xhx2.frame=originRect;
[self.view addSubview:_xhx2];
}
// 设置登录按钮
{
CGRect originRect;
CGRect Rect=_xhx2.frame;
originRect.origin.x = Rect.origin.x;
originRect.origin.y = Rect.origin.y+Rect.size.height+20;
originRect.size.width = Rect.size.width;
originRect.size.height = _passWord.frame.size.height+10;
_login=[[UIButton alloc] initWithFrame:originRect];
_login.contentMode = UIViewContentModeScaleAspectFit;
[_login setBackgroundImage:[UIImage imageNamed:@"btn_登录_n.png"] forState:normal];
[_login addTarget:self action:@selector(log:) forControlEvents:UIControlEventTouchUpInside];
[_login setTag:100];
[self.view addSubview:_login];
}
// 设置注册按钮
{
CGRect Rect=_login.frame;
CGRect originRect;
originRect.origin.x = Rect.origin.x;
originRect.origin.y = Rect.origin.y+Rect.size.height+10;
originRect.size.height = Rect.size.height;
originRect.size.width = Rect.size.width;
_registerButton = [[UIButton alloc] initWithFrame:originRect];
_registerButton.contentMode = UIViewContentModeScaleAspectFit; // 设置内容填充模式
[_registerButton setBackgroundImage:[UIImage imageNamed:@"btn_注册_n.png"] forState:normal];
[_registerButton addTarget:self action:@selector(log:) forControlEvents:UIControlEventTouchUpInside];
[_registerButton setTag:300];
[self.view addSubview:_registerButton];
}
// 设置取消按钮
{
CGRect originRect;
CGRect Rect=_user.frame;
originRect.origin.x=Rect.origin.x+Rect.size.width;
originRect.origin.y=Rect.origin.y;
originRect.size.width=Rect.size.height;
originRect.size.height=Rect.size.height;
_cancel=[[UIButton alloc] initWithFrame:originRect] ;
[_cancel setBackgroundImage:[UIImage imageNamed:@"btn_首页用户名取消@2x.png"] forState:normal]; // 为按钮设置背景图片
// 必须为按钮绑定监听方法addTarget
[_cancel addTarget:self action:@selector(log:) forControlEvents:UIControlEventTouchUpInside];
[_cancel setTag:200];
[self.view addSubview:_cancel];
}
// 设置错误提示标签
{
CGRect rect;
rect.origin.x=self.label1.frame.origin.x;
rect.origin.y=self.label1.frame.origin.y-(self.label1.frame.size.height+5);
rect.size.height=self.label1.frame.size.height;
rect.size.width=self.label1.frame.size.width+self.user.frame.size.width;
self.error=[[UILabel alloc] initWithFrame:rect];
[self.error setText:@"欢迎登录!!!"];
self.error.textColor =[UIColor greenColor];
[self.error setTextAlignment:NSTextAlignmentCenter]; // 设置标签内的文字在标签内的对齐方式
[self.view addSubview:_error];
}
}
// 以下设置输入框的代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField // 当输入框成为第一响应者
{
switch (textField.tag) {
case 400:
self.error.text=@"请输入您的用户名";
break;
case 500 :
self.error.text=@"请输入6位密码";
break;
default:
break;
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
switch (textField.tag) {
case 400:
if (self.user.text.length==0) {
self.error.text=@"用户名不能为空";
self.allowUser = NO;
}else
self.allowUser=YES;
break;
case 500 :
if (self.passWord.text.length!=6) {
if(self.allowUser == YES)
{
self.error.text=@"密码输入不合法";
}
else
self.error.text=@"密码和用户名均不合法";
self.allowPass = NO;
}else
self.allowPass = YES;
break;
default:
break;
}
}
-(void)log:(UIButton *)sender
{
NSString *user = _user.text;
NSString *pass = _passWord.text;
ViewController3 *myV3=[[ViewController3 alloc] init];
switch (sender.tag)
{
case 100:
if (([user isEqual:@"HuaShan"]) &&([pass isEqual:@"123456"])&&_allowPass&&_allowUser)
{
NSLog(@"登录成功");
self.passWord.text=nil;
ViewController2 *myVC2 = [[ViewController2 alloc] init];
[self presentViewController:myVC2 animated:YES completion:^{
}];
}
else
{
self.error.text=@"登录失败,请重新登录";
NSLog(@"登录失败");
}
self.passWord.text = nil;
break;
case 200:
_user.text=nil;
NSLog(@"cancel");
break;
case 300:
[self presentViewController:myV3 animated:YES completion:^{
}];
break;
default:
break;
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [_user resignFirstResponder];
// [_passWord resignFirstResponder];
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
新建控制器类viewController2
编辑viewController2.h如下:
//
// ViewController2.h
// denglu
//
// Created by apple on 15/8/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong)UILabel *label;
@end
编辑viewController2.m如下:
//
// ViewController2.m
// denglu
//
// Created by apple on 15/8/26.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController2.h"
#define width [UIScreen mainScreen].bounds.size.width
#define height [UIScreen mainScreen].bounds.size.height
@interface ViewController2 ()
@end
@implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
{
// label显示第几页
CGRect rect=CGRectMake(width/2, height*0.8, width/5, height/8);
self.label = [[UILabel alloc] initWithFrame:rect];
[_label setTextColor:[UIColor whiteColor]];
[_label setBackgroundColor:[UIColor clearColor]];
[_label setText:@"第 1 页"];
CGPoint labelCenter=_label.center;
labelCenter.x=width/2;
_label.center=labelCenter;
// UIScrollView
CGRect rect1=CGRectMake(0, 0, width, height);
_scroll=[[UIScrollView alloc] initWithFrame:rect1];
_scroll.pagingEnabled=YES; // 设置可以翻页
_scroll.bounces=NO; // 设置不带弹性
for (int i=0; i<4; i++) {
UIImageView *imav=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, height)];
imav.image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]];
[_scroll addSubview:imav];
}
_scroll.contentSize=CGSizeMake(width*4, height);
_scroll.showsHorizontalScrollIndicator=NO;
_scroll.showsVerticalScrollIndicator=NO;
_scroll.delegate=self;
[self.view addSubview:_scroll];
[ self.view addSubview:_label];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[_label setText:@"开始移动"];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int i=floor(scrollView.contentOffset.x/width)+1;
[_label setText:[NSString stringWithFormat:@"第 %d 页",i]];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_label setText:@"正在移动"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
新建控制器类viewController3
编辑viewController3.h如下:
//
// ViewController3.h
// denglu
//
// Created by apple on 15/8/27.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController3 : UIViewController<UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *scroll;
@end
编辑viewController3.m如下:
//
// ViewController3.m
// denglu
//
// Created by apple on 15/8/27.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController3.h"
#define height [UIScreen mainScreen].bounds.size.height
#define width [UIScreen mainScreen].bounds.size.width
@interface ViewController3 ()
@end
@implementation ViewController3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
// 设置上下滑动
CGRect Rect=CGRectMake(0, 0, width, height);
self.scroll=[[UIScrollView alloc] initWithFrame:Rect];
for (int i = 0; i<4; i++) {
UIImageView *imav=[[UIImageView alloc ] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]]];
imav.frame=CGRectMake(0, i*height, width, height);
[self.scroll addSubview:imav];
}
self.scroll.pagingEnabled=YES; // 设置翻页模式
self.scroll.showsVerticalScrollIndicator=NO; // 设置不显示竖直滑动条
self.scroll.bounces=NO; // 设置禁止弹性设置
self.scroll.contentSize=CGSizeMake(width, height*4); // 必须将内容的大小告知UIScrollView的contentSize属性
[self.view addSubview:self.scroll]; // 将ScrollView添加到VIew中
}
- (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
运行结果如下: