代码

1.oc基本语法

//

//  main.m

//  oc基本语法

//

//  Created by lanou on 16/7/9.

//  Copyright © 2016年lanou. All rights reserved.

//

#import

int main(int argc, const char * argv[]) {

@autoreleasepool {

// insert code here...

NSLog(@"Hello, World!");

//整型

NSInteger a = 10;

NSLog(@"a = %ld",a);

//浮点型

CGFloat b = 2.3;

NSLog(@"b = %.2f",b);

//布尔类型

//BOOL  flag = YES;

//字符串NSString(只要是对象类型,占位符全是%@)

NSString *str = @"abcde";

NSLog(@"str = %@",str);

//length字符串的长度

NSLog(@"str的长度= %ld",str.length);

//字符串相等(全等,前缀,后缀)

//1全等isEqualToString:

if ([str isEqualToString:@"abcde"]) {

NSLog(@"是的");

}

//2前缀相等hasPrefix

if([str hasPrefix:@"a"]){

NSLog(@"前缀等于a");

}

//3后缀相等hasSuffix

if([str hasSuffix:@"de"]){

NSLog(@"后缀等于de");

}

//格式化创建字符串

NSString *str1 = [NSString stringWithFormat:@"%@++++++",str];

NSLog(@"str1 = %@",str1);

}

return 0;

}

2.OC基础语法

//

//  main.m

//  OC基础语法

//

//  Created by lanou on 16/7/9.

//  Copyright © 2016年lanou. All rights reserved.

//

#import

int main(int argc, const char * argv[]) {

@autoreleasepool {

//数组(NSArray/NSMutableArray不可变数组/可变数组)

//  1不可变数组NSArray

NSArray *array1 = @[@"a",@"b",@"c",@"d"];

//创建

NSLog(@"array1 = %@",array1);

//数组元素个数.count

NSLog(@"count = %ld",array1.count);

//通过下标访问数组里面的元素

NSString *str = array1[0];

NSLog(@"str = %@",str);

//  2可变数组NSMutableArray

NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];

NSLog(@"mutableArray = %@",mutableArray);

//元素个数:count

//添加元素addObject

[mutableArray addObject:@"5"];

NSLog(@"已添加-----%@",mutableArray);

//移除元素removeObject

[mutableArray removeObject:@"3"];

NSLog(@"已移除---------%@",mutableArray);

//字典(存放多个键值对的一种数据类型)(NSDictionary不可变,NSMutableDictionary可变)

//不可变字典

NSDictionary *dict = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};

NSLog(@"dict = %@",dict);

NSString *string = [dict objectForKey:@"key1"];

NSLog(@"string = %@",string);

//所有的key值,所有的value值

NSLog(@"allkeys = %@,allvalue = %@",dict.allKeys,dict.allValues);

}

return 0;

}

3.oc方法格式

//

//  ViewController.m

//  oc方法格式

//

//  Created by lanou on 16/7/9.

//  Copyright © 2016年lanou. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//视图加载好的时候调用

- (void)viewDidLoad {

[super viewDidLoad];

//调用方法用空格,方法调用结束用中括号表示

[self func1];

NSInteger num = [self func2];

NSLog(@"num = %ld",num);

NSInteger length = [self lengOfString:@"12345"];

NSLog(@"length = %ld",length);

}

//OC方法的格式:

//    +表示类方法,只能用类来调用,

//    -表示实例方法,用对象调用

//无参数输入的方法格式:+/- (方法的返回值)方法名

-(void)func1{

NSLog(@"%s",__func__);

}

-(NSInteger)func2{

NSLog(@"%s",__func__);

return 20;

}

//有参数输入的方法格式:+/-(方法的返回值)方法名:(参数1类型)参数1名:(参数2类型)参数2名

//输入字符串,返回字符串的长度

-(NSInteger)lengOfString:(NSString *)string

{

return string.length;

}

//内存溢出的时候调用

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

4.UI常用控件

//

//  ViewController.m

//  UI常用控件

//

//  Created by lanou on 16/7/10.

//  Copyright © 2016年lanou. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UILabel *titleLable;

//左边按钮

@property(nonatomic,strong)UIButton *leftBtn;

//右边按钮

@property(nonatomic,strong)UIButton *rightBtn;

//显示图片

@property(nonatomic,strong)UIImageView *myImageView;

@property(nonatomic,strong)NSArray *imageNames;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//创建数组

self.imageNames = @[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

//设置标签坐标高度

self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 150, 30)];

//设置标签文本

self.titleLable.text = @"biaoqingdi";

//添加到视图

[self.view addSubview:self.titleLable];

//设置左边按钮坐标高度

self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];

//关闭交互

self.leftBtn.userInteractionEnabled = NO;

//根据名字显示图片

UIImage *leftImage = [UIImage imageNamed:@"left_disable"];

//给按钮指定背景图片

[self.leftBtn setBackgroundImage:leftImage forState:(UIControlStateNormal)];

//添加到界面

[self.view addSubview:self.leftBtn];

//监听按钮

[self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

//设置相框坐标高度

self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(85 , 100, 200, 200)];

//设置显示的图片

UIImage *image = [UIImage imageNamed:@"biaoqingdi"];

self.myImageView.image = image;

//添加到界面

[self.view addSubview:self.myImageView];

//设置右边按钮坐标高度

self.rightBtn =[[UIButton alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];

//根据名字显示图片

UIImage *rightimage = [UIImage imageNamed:@"right_normal"];

//给按钮指定背景图片

[self.rightBtn setBackgroundImage:rightimage forState:(UIControlStateNormal)];

//添加到界面

[self.view addSubview:self.rightBtn];

//监听按钮

[self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

}

-(void)rightBtnAction{

//切换到下一张图片

//获取当前是第几张图片

NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];

//不是最后一张才切换到下一张

if(index < 4){

if (index == 3) {

//改变右边按钮颜色和关闭交互

//关闭交互

self.rightBtn.userInteractionEnabled = NO;

//加载图片

UIImage *image = [UIImage imageNamed:@"right_disable"];

//指定背景图片

[self.rightBtn setBackgroundImage:image forState:(UIControlStateNormal)];

}else{

//左边按钮和右边按钮都是在一个正常状态

//交互开启

self.leftBtn.userInteractionEnabled = YES;

self.rightBtn.userInteractionEnabled = YES;

//加载图片

UIImage *leftNormal = [UIImage imageNamed:@"left_normal"];

UIImage *rightNormal = [UIImage imageNamed:@"right_normal"];

//设置背景图片

[self.leftBtn setBackgroundImage:leftNormal forState:(UIControlStateNormal)];

[self.rightBtn setBackgroundImage:rightNormal forState:(UIControlStateNormal)];

}

//说明下一个标签

NSString *nextTitle = self.imageNames[index+1];

//获取标签名称

self.titleLable.text = nextTitle;

//切换到下一张图片

self.myImageView.image = [UIImage imageNamed:nextTitle];

}

}

-(void)leftBtnAction{

//切换到上一张图片

//获取当前是第几张图片

NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];

//不是第一张才切换到上一张

if(index > 0){

if (index == 1) {

//左边按钮交互关闭,图片切换

//关闭交互

self.leftBtn.userInteractionEnabled = NO;

//加载图片

UIImage *image = [UIImage imageNamed:@"left_disable"];

//设置背景图片

[self.leftBtn setBackgroundImage:image forState:(UIControlStateNormal)];

}else{

//左右两边按钮都是正常状态

//左边按钮和右边按钮都是在一个正常状态

//交互开启

self.leftBtn.userInteractionEnabled = YES;

self.rightBtn.userInteractionEnabled = YES;

//加载图片

UIImage *leftNormal = [UIImage imageNamed:@"left_normal"];

UIImage *rightNormal = [UIImage imageNamed:@"right_normal"];

//设置背景图片

[self.leftBtn setBackgroundImage:leftNormal forState:(UIControlStateNormal)];

[self.rightBtn setBackgroundImage:rightNormal forState:(UIControlStateNormal)];

}

//说明上一个标签

NSString *preTitle = self.imageNames[index-1];

//获取标签名称

self.titleLable.text = preTitle;

//切换到上一张图片

self.myImageView.image = [UIImage imageNamed:preTitle];

}

}

- (void)demo{

//按钮UIButton

//UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 80, 80)];

//frame表明了控件的坐标和宽高(CGRect类型)

[button setTitle:@"眼镜哥" forState:UIControlStateNormal];

//根据名字加载图片

UIImage *image = [UIImage imageNamed:@"right_normal"];

//给按钮指定背景图片

[button setBackgroundImage:image forState:UIControlStateNormal];

button.backgroundColor = [UIColor redColor];

//按钮的监听

[button addTarget:self action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpInside];

//添加到视图上面

[self.view addSubview:button];

//相框UIImageView

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 70, 200, 200)];

UIImage *image1 = [UIImage imageNamed:@"biaoqingdi"];

//设置imageView显示的图片

imageView.image = image1;

[self.view addSubview:imageView];

//标签UILable

UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(150, 30, 150, 30)];

//设置标签的文本

lable.text = @"表情弟";

//设置居中方式

lable.textAlignment = NSTextAlignmentCenter;

lable.textColor = [UIColor redColor];

[self.view addSubview:lable];

}

-(void)btnClickLister{

NSLog(@"click btn");

}

@end

5.汤姆猫

//

//  ViewController.m

//汤姆猫

//

//  Created by lanou on 16/7/12.

//  Copyright © 2016年lanou. All rights reserved.

//

#import"ViewController.h"

@interfaceViewController()

@property(weak,nonatomic)IBOutletUIImageView*tomCatView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

}

- (IBAction)FootLeftAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"FootLeft"WithCount:30];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 30;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"FootLeft_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 30*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)footRightAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"footRight"WithCount:30];

/* //创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 30;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"footRight_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 30*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)eatBirdAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"eat"WithCount:40];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 40;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"eat_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 40*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

-(void)tomCatAnimationWithName:(NSString*)name WithCount:(NSInteger)count;

{

//判断是否正在执行

if([self.tomCatViewisAnimating]){

return;

}

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray*images = [NSMutableArrayarray];

//借助i循环创建图片

for(NSIntegeri =0;i < count;i++) {

//将name拼接

NSString*imageName = [NSStringstringWithFormat:@"%@_%02ld.jpg",name,i];

//根据格式化的图片名加载图片image

UIImage*image = [UIImageimageNamed:imageName];

//将图片image添加到数组images中

[imagesaddObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages= images;

//设置动画时长

self.tomCatView.animationDuration= count*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount=1;

//开始动画

[self.tomCatViewstartAnimating];

}

- (IBAction)pieAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"pie"WithCount:24];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 24;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"pie_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 24*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)stomachAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"stomach"WithCount:34];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 34;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"stomach_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 34*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)knockoutAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"knockout"WithCount:81];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 81;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"knockout_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 81*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)angryAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"angry"WithCount:26];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 26;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"angry_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 26*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)scratchAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"scratch"WithCount:56];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 56;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"scratch_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 56*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)cymbalAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"cymbal"WithCount:13];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 13;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"cymbal_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 13*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)fartAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"fart"WithCount:28];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 28;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"fart_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 28*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (IBAction)drinkMilkAction:(UIButton*)sender {

//汤姆猫根据名字和数量执行动画

[selftomCatAnimationWithName:@"drink"WithCount:81];

/*

//创建可变数组images,负责存放要播放的图片数组

NSMutableArray *images = [NSMutableArray array];

for (NSInteger i = 0;i < 81;i++) {

//根据i来加载图片,然后添加到可变数组images里面

NSString *imageName = [NSString stringWithFormat:@"drink_%02ld.jpg",i];

//根据格式化的图片名加载图片image

UIImage *image = [UIImage imageNamed:imageName];

//将图片image添加到数组images中

[images addObject:image];

}

//设置动画图片数组

self.tomCatView.animationImages = images;

//设置动画时长

self.tomCatView.animationDuration = 81*0.075;

//设置动画重复次数

self.tomCatView.animationRepeatCount = 1;

//开始动画

[self.tomCatView startAnimating];

*/

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

6.访问系统相册

//

//  ViewController.m

//访问系统相册

//

//  Created by lanou on 16/7/12.

//  Copyright © 2016年lanou. All rights reserved.

//

#import"ViewController.h"

//遵守协议

@interfaceViewController()

@property(nonatomic,strong)UIButton*userBtn;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//创建初始化

//所以的能看到的UI控件创建初始化方式都可以采用alloc initWithFrame

self.userBtn= [[UIButtonalloc]initWithFrame:CGRectMake(175,60,80,80)];

//设置颜色

self.userBtn.backgroundColor= [UIColorredColor];

//设置背景图片

//给按钮指定背景图片

UIImage*userImage = [UIImageimageNamed:@"login_header@2x"];

//根据名字显示图片

[self.userBtnsetBackgroundImage:userImageforState:(UIControlStateNormal)];

//设置圆形半径

self.userBtn.layer.cornerRadius=40;

//圆形超过方框的部分切割掉

self.userBtn.layer.masksToBounds=YES;

//添加点击事件:去访问系统相册

[self.userBtnaddTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)];

//添加到屏幕上面来

[self.viewaddSubview:self.userBtn];

}

//访问系统相册

-(void)setUserImage{

//创建系统相册

UIImagePickerController*imagePicker = [[UIImagePickerControlleralloc]init];

//设置代理,到@interface后面遵守协议,拷贝

imagePicker.delegate=self;

//弹出系统相册

[selfpresentViewController:(imagePicker)animated:YEScompletion:

nil];

}

//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary *)editingInfo

{

//设置头像

[self.userBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];

//将系统相册消失掉

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

7.轮播图

//

//  ViewController.m

//轮播图

//

//  Created by lanou on 16/7/13.

//  Copyright©2016年lanou. All rights reserved.

//

#import"ViewController.h"

//

@interfaceViewController()

//宏定义

#define screenWidth [UIScreen mainScreen].bounds.size.width

#define screenHeight [UIScreen mainScreen].bounds.size.height

//滑动视图UIScrollView,自带了可滑动功能

@property(nonatomic,strong)UIScrollView*scrollView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//屏幕宽高

//创建初始化滑动视图

self.scrollView= [[UIScrollViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

for(NSIntegeri =0; i <6; i++) {

//根据i循环创建UIImageView,再添加到滑动视图UIScrollView上面

UIImageView*imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(i*screenWidth,0,screenWidth,screenHeight)];

NSString*imagename =nil;

if(i==5) {

imagename =@"1.jpg";

}else{

imagename = [NSStringstringWithFormat:@"%ld.jpg",i+1];

}

//加载响应的图片

UIImage*image = [UIImageimageNamed:imagename];

//设置图片

imageView.image= image ;

//将imageview添加到滑动视图上面

[self.scrollViewaddSubview:imageView];

//设置滑动视图的滑动区域contentSize

self.scrollView.contentSize=CGSizeMake(6*screenWidth,screenHeight);

//整屏翻转

self.scrollView.pagingEnabled=YES;

//边界回弹

self.scrollView.bounces=NO;

//设置代理,代理是负责监听滑动是要整个过程的

self.scrollView.delegate=self;

}

//添加到滑动视图屏幕上面

[self.viewaddSubview:self.scrollView];

}

//滑动视图做滑动的时候调用

- (void)scrollViewDidScroll:(UIScrollView*)scrollView

{

//contentoffset是访问到了滑动视图的偏移量,包含了x,和y轴的偏移量

//setContentOffset:animated:

NSLog(@"offset.x = %f,offset.y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

8.弹框

//

//  ViewController.m

//弹框

//

//  Created by lanou on 16/7/13.

//  Copyright © 2016年lanou. All rights reserved.

//

//UITextField:怎么获取UITextField的文本,怎么设置输入框键盘类型,设置占位字符串,密文输入

#import"ViewController.h"

@interfaceViewController()

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

}

- (IBAction)alertAction:(id)sender {

//弹框:UIAlertController

//Title:大标题

//message:小标题

//preferredStyle:弹框样式

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"你有病"message:@"你可以去吃药了"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction*okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*_Nonnullaction) {

//按钮按下要做的事情

NSLog(@"淮南师范学院");

}];

//取消

UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:nil];

//给弹框添加行为

[alertControlleraddAction:okAction];

[alertControlleraddAction:cancelAction];

[selfpresentViewController:alertControlleranimated:YEScompletion:nil];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

9.登录界面

//

//  ViewController.m

//界面

//

//  Created by lanou on 16/7/14.

//  Copyright © 2016年lanou. All rights reserved.

//

#import"ViewController.h"

//遵守协议

@interfaceViewController ()

@property(nonatomic,strong)UIButton*userBtn;

@property(nonatomic,strong)UILabel*titleLabel1;

@property(nonatomic,strong)UILabel*titleLabel2;

@property(nonatomic,strong)UITextField*TextField1;

@property(nonatomic,strong)UITextField*TextField2;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//设置头像按钮坐标宽高

self.userBtn= [[UIButtonalloc]initWithFrame:CGRectMake(150,80,100,100)];

//根据名字显示图片

UIImage*image1 = [UIImageimageNamed:@"login_header"];

[self.userBtnsetBackgroundImage:image1forState:(UIControlStateNormal)];

//设置圆形半径

self.userBtn.layer.cornerRadius=50;

//圆形超过方框的部分切割掉

self.userBtn.layer.masksToBounds=YES;

//添加点击事件:去访问系统相册

[self.userBtnaddTarget:selfaction:@selector(setuserimage)forControlEvents:(UIControlEventTouchUpInside)];

//将按钮添加到屏幕上来

[self.viewaddSubview:self.userBtn];

//设置标签按钮坐标宽高

self.titleLabel1= [[UILabelalloc]initWithFrame:CGRectMake(30,250,150,25)];

//设置标签文本

self.titleLabel1.text=@"账号";

//将标签添加到屏幕上

[self.viewaddSubview:self.titleLabel1];

//设置文本字段坐标宽高

self.TextField1= [[UITextFieldalloc]initWithFrame:CGRectMake(100,250,250,25)];

//设置文本字段提示文字

self.TextField1.placeholder=@"请输入账号";

//开启密文

self.TextField2.secureTextEntry=YES;

//设置键盘类型(默认键盘)

self.TextField1.keyboardType=UIKeyboardTypeDefault;

//将文本字段添加到屏幕上

[self.viewaddSubview:self.TextField1];

//设置标签坐标宽高

self.titleLabel2= [[UILabelalloc]initWithFrame:CGRectMake(30,300,150,25)];

//设置标签文本

self.titleLabel2.text=@"密码";

//将标签添加到屏幕上

[self.viewaddSubview:self.titleLabel2];

//设置文本字段坐标宽高

self.TextField2= [[UITextFieldalloc]initWithFrame:CGRectMake(100,300,250,25)];

//设置文本字段提示文字

self.TextField2.placeholder=@"请输入密码";

//开启密文

self.TextField2.secureTextEntry=YES;

//设置键盘类型(数字键盘)

self.TextField2.keyboardType=UIKeyboardTypeNumberPad;

//将文本字段添加到屏幕上

[self.viewaddSubview:self.TextField2];

}

//访问系统相册

-(void)setuserimage{

//创建系统相册

UIImagePickerController*imagepicker = [[UIImagePickerControlleralloc]init];

//设置代理,到@interface后面遵守协议,拷贝

imagepicker.delegate=self;

//弹出系统相册

[selfpresentViewController:imagepickeranimated:YEScompletion:nil];

}

//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary *)editingInfo

{

//设置头像

[self.userBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];

//将系统相册消失掉

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

- (IBAction)loadAction:(UIButton*)sender {

//判断文本字段是否为空

if(self.TextField1.text.length==0||self.TextField2.text.length==0)

{

//设置弹框内容

UIAlertController*alertcontroller = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"账号或密码不能为空"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okaction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

UIAlertAction* cancleaction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

//给按钮添加行为

[alertcontrolleraddAction:okaction];

[alertcontrolleraddAction:cancleaction];

[selfpresentViewController:alertcontrolleranimated:YEScompletion:nil];

}

else

NSLog(@"%@,%@",_TextField1,_TextField2);

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

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

推荐阅读更多精彩内容