UI学习之常用方法
1.-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
if ([delegate respondsToSelector:@selector(didFinishUpload:)]) {
[delegate didFinishUpload:self];
}
视图篇
1.[self presentViewController:jump animated:YES completion:nil];//视图跳转[self dismissViewControllerAnimated:YES completion:nil];//视图回跳
2.self.window .rootViewController = loginVC;给window添加根视图控制器。
3. recruitmentViewController *recruitment = [[recruitmentViewController alloc] init];
[UIApplication sharedApplication].delegate.window.rootViewController = recruitment;在非
#import "AppDelegate.h"中给window添加根视图控制器。
4. [self.window makeKeyAndVisible];//显示(使其可见)
5.self.window.frame = [[UIScreen mainScreen] bounds];//指定window大小跟屏幕大下一致
6. [viewF insertSubview:view2 atIndex:1];在viewF第n层插入一个子视图
7. view3.alpha = 0.3;//透明度 view3.hidden = NO;//是否隐藏,隐藏不需要的的视图
8. [viewF insertSubview:view3 aboveSubview:view4];将view3子视图添加到view4子视图下
[viewF insertSubview:view3 belowSubview:view4];//反之
9. [viewF exchangeSubviewAtIndex:2 withSubviewAtIndex:3];//交换两个位置的视图(父视图上面的第一层为1)
10. [viewF sendSubviewToBack:view4];//将某个子视图移到父视图的最底层(在确保view4已经添加到了视图中的前提下)
10. UIView *view5 = [viewF viewWithTag:3];//获取父视图中标签为3的视图 view5.backgroundColor = [UIColor blackColor];
11. view4.userInteractionEnabled = YES;//是否允许用户点击(默认YES),如果设置成no,子视图不会覆盖父视图的点击事件
self.view.userInteractionEnabled = YES;//如果父视图不允许交互,那么子视图的事件也会被屏蔽
12. b.center = CGPointMake(180, 300);//以(180, 300)为视图中心中心重新定位
13. label.font = [UIFont systemFontOfSize:18];//系统字体
14./label.font = [UIFont boldSystemFontOfSize:18];//系统字体加粗
15.label.font = [UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>];//自定义字体,第一个参数代表字体名,第二个参数代表大小
16.label.numberOfLines = 0;//设置为可现显示任意行
label.lineBreakMode = NSLineBreakByWordWrapping;//换行样式
17.label.textAlignment = NSTextAlignmentRight;//对齐方式
18.label.shadowColor = [UIColor blackColor];//文本的阴影颜色
19.label.shadowOffset = CGSizeMake(2, 3);//文本的阴影偏移
20. label.enabled = NO;//是否激活,默认YES
21.
- lineBreakMode:设置标签文字过长时的显示方式。
- label.lineBreakMode = NSLineBreakByCharWrapping; //以字符为显示单位显示,后面部分省略不显示。
- label.lineBreakMode = NSLineBreakByClipping; //剪切与文本宽度相同的内容长度,后半部分被删除。
- label.lineBreakMode = NSLineBreakByTruncatingHead; //前面部分文字以……方式省略,显示尾部文字内容。
- label.lineBreakMode = NSLineBreakByTruncatingMiddle; //中间的内容以……方式省略,显示头尾的文字内容。
- label.lineBreakMode = NSLineBreakByTruncatingTail; //结尾部分的内容以……方式省略,显示头的文字内容。
- label.lineBreakMode = NSLineBreakByWordWrapping; //以单词为显示单位显示,后面部分省略不显示。
button.layer.masksToBounds = YES;//多余部分剪切掉
按钮篇
1. UISwitch *switch1 = [[UISwitch alloc] initWithFrame:CGRectMake(30, 50, 100, 90)];//高度
2. switch1.onTintColor = [UIColor blueColor];//
3.switch1.tintColor = [UIColor redColor];
4.switch1.thumbTintColor = [UIColor purpleColor];
5. switch1.on = YES;//设置开关初始状态
6. [switch1 addTarget:self action:@selector(mySwitch:) forControlEvents:UIControlEventValueChanged];//绑定监听事件
7. UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"122",@"123",@"121"]];
8.seg.frame = CGRectMake(20, 40, 200, 40);
9. NSLog(@"段数:%zi",seg.numberOfSegments);
10. [seg insertSegmentWithImage:[UIImage imageNamed:@"avatar"] atIndex:3 animated:YES];//插入图片,图片大小应合适
11. [seg setTitle:@"分段" forSegmentAtIndex:3];//给某一段设置标题
12.NSString *s = [seg titleForSegmentAtIndex:1];//获取标题
13.UIImage *image = [seg imageForSegmentAtIndex:3];//获取图片
14.[seg setWidth:100 forSegmentAtIndex:2];//设置某段的宽
15.[seg setEnabled:NO];//整体不被激活
16.[seg setEnabled:NO forSegmentAtIndex:1];//设置某一段是否被激活
17.seg.selectedSegmentIndex = 1;//设置默认选中项
18. NSLog(@"选中:%zi",seg.selectedSegmentIndex);//获取当前被选中的段
19. seg.tintColor = [UIColor redColor];//边框和选中颜色
20.[seg setBackgroundImage:[UIImage imageNamed:@"btn_select"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
21.[seg addTarget:self action:@selector(segmentChange) forControlEvents:UIControlEventValueChanged];//绑定监听事件
22.UISegmentedControl *seg1 = [[UISegmentedControl alloc] initWithFrame:CGRectMake(20, 100, 200, 40)];
23.[seg1 insertSegmentWithTitle:@"分段" atIndex:1 animated:YES];
24.[seg1 insertSegmentWithTitle:@"分段1" atIndex:0 animated:YES];//增加
25.[seg1 removeSegmentAtIndex:1 animated:YES];//移除
Button:
1.[myButton.layer setCornerRadius:20.0];//设置圆角半径
2.myButton.frame = CGRectMake(15+90*j, 150+60*i, 70, 40);
3.myButton.backgroundColor = [UIColor orangeColor];//橙黄色
4.myButton.titleLabel.font = [UIFont systemFontOfSize: 30]; ;//字体大小
5.[myButton setTitle:ButtonTag[i*4+j] forState:UIControlStateNormal];//设置标题和
6.button.showsTouchWhenHighlighted = YES;//点击高亮
7.[myButton setImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];//设置图片
8.[headLeftLabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//button文字对齐方式
图片篇
1. UIImage *img =[UIImage imageNamed:@"1.jpg"]; NSLog(@"%@",img);
2. UIImage *img1 = [UIImage imageWithContentsOfFile:@"/Users/yangjie/Pictures/3.jpg"];//通过路径获取图片
3. NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://g.hiphotos.baidu.com/zhidao/pic/item/9f510fb30f2442a79fbc68ded343ad4bd113021e.jpg"]];//通过URL拿到图片的数据
4.UIImage *img2 = [UIImage imageWithData:imgData];
5. NSData *data1 = UIImageJPEGRepresentation(img2, 1);//图片转换成data,第一个是要转换的图片对象,第二个参数代表压缩质量(0~1浮点,1代表不压缩)
6. NSLog(@"--%zi",data1.length);
7. NSData *data2 = UIImagePNGRepresentation(img1);//图片转换成data
8.UIImageView *imgView = [[UIImageView alloc] initWithImage:img2];
imgView.frame = CGRectMake(20, 30, 200, 200);
9.float w = img2.size.width;//获取图片的宽 float h = img2.size.height;//获取图片的高
菜单篇
1..动态菜单
//UIViewController<UIActionSheetDelegate>//遵循动态菜单代理模式
- (void) login
{
NSLog(@"点击");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delegate Example" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destruct" otherButtonTitles:@"BUtton 1" , @"BUtton 2" , @"BUtton 3" , nil];
[actionSheet showInView:self.tabBarController.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: NSLog(@"Action Destructed!") ;break;
case 1: NSLog(@"Action Button 1 Clicked!");break;
case 2: NSLog(@"Action Button 2 Clicked!");break;
case 3: NSLog(@"Cancel Button Clicked!"); break;
default: break;
}
}
2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提示内容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"按钮1",@"按钮2", nil];//alert创建
3. alert.title = @"123";//设置标题
4. alert.message = @"11111";//设置内容
5. [alert show];//使其能显示
6. [alert dismissWithClickedButtonIndex:0 animated:YES];//消失alert
画图篇
1.自定义画图,类似android的onDraw()
- (void)drawRect:(CGRect)rect;
is invoked automaticall,never call it directly!!
2.刷新视图,类似android的invalidate()
- (void)setNeedsDisplay;
When a view needs to be redrawn,use:
3.在非主线程中调用,需使用如下方法:
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
4.setNeedsDisplay是不阻塞的,
需要自己制造阻塞,
setNeedsDisplay我理解就是告诉系统,等会帮哥把这块重新画一下。
系统就知道了,等系统有空了,他就一起画了,
如果想立即画出来,可能要用setNeedsLayout,
或者不用drawInRect系列的方式,直接改view.image或者文字,加动画等方式实现吧。
5.setNeedsDisplay和layoutSubViews
首先两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调 用layoutSubViews,就可以处理子视图中的一些数据。
宗上所诉,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。
eg:
#import "TuYa.h" @implementation TuYa
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
_lineArray = [[NSMutableArray alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(, , , );
[button setTitle:@"撤销" forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(undoDraw) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
//每次开始触摸的时候增加一个新数组,数组记录画出的点,这些点用于回话
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_lineArray addObject:[NSMutableArray arrayWithCapacity:]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue *value = [NSValue valueWithCGPoint:point];
[[_lineArray lastObject] addObject:value];//给最后一根线加点
[self setNeedsDisplay];//setNeedsDisplay会调用自动调用drawRect方法
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();//获得上下文
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//画笔颜色
for( int i = ; i < _lineArray.count ; i ++ )
{
NSMutableArray *pointArray = [[NSMutableArray alloc] initWithArray:[_lineArray objectAtIndex:i]];
for( int j = ; j < pointArray.count- ; j ++ )
{
NSValue *a = pointArray[j];
CGPoint b = [a CGPointValue];
NSValue *c = pointArray[j+];
CGPoint d = [c CGPointValue]; CGContextMoveToPoint(context, b.x, b.y);
CGContextAddLineToPoint(context, d.x, d.y);
}
}
CGContextStrokePath(context);
}
- (void)undoDraw
{
[_lineArray removeLastObject];
//会自动调用UIView的drawRect方法
[self setNeedsDisplay];//重绘
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end
程序篇
1.UITextField
#import "ViewController.h" @interface ViewController ()<UITextFieldDelegate>//重写代理方法观看TextFiled执行过程
{
UITextField *field;
UITextField *field1;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];//背景必须要设置
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];//设置背景图片
//创建自定义键盘
UIView *keyView = [[UIView alloc]init];
keyView.frame = CGRectMake(, , , );
keyView.backgroundColor = [UIColor redColor];
//对照
field1 = [[UITextField alloc] init];
field1.frame = CGRectMake(, , , );
field1.backgroundColor = [UIColor whiteColor];
field1.placeholder = @"占位符";//占位符
//实验
field = [[UITextField alloc] init];
field.frame = CGRectMake(, , , );
field.backgroundColor = [UIColor whiteColor];
field.placeholder = @"占位符";//占位符 field.borderStyle = UITextBorderStyleNone ;//边框风格
/*
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone,
UITextBorderStyleLine,//黑框
UITextBorderStyleBezel,//立体
UITextBorderStyleRoundedRect//圆角
};
*/ field.keyboardType = UIKeyboardTypeDefault;//设置键盘类型
/*
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // A number pad with a decimal point.
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // A default keyboard type with URL-oriented addition (shows space . prominently). UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated };
*/
field.returnKeyType = UIReturnKeyYahoo;//设置键盘return键样式
/*
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
};
*/ field.background = [UIImage imageNamed:@""];//添加背景 field.clearButtonMode = UITextFieldViewModeWhileEditing;//文明本后面加个"x"清除文本
/*
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,"X"从不出现
UITextFieldViewModeWhileEditing,"X"编辑本文本时出现,否则消失
UITextFieldViewModeUnlessEditing,"X"编辑本文本时消失,否则出现
UITextFieldViewModeAlways//"X"永远存在
};
*/
field.clearsOnBeginEditing = YES; //重新点击,清除所有文本内容 // field.leftView = myView;//在文本左侧添加一视图
field.leftViewMode = UITextFieldViewModeNever;
/*
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
*/ field.inputView = keyView;//当文本框在编辑时的弹出视图,类似键盘 field.delegate = self;//设置代理(由当前视图来执行)下边重写代理方法
field1.delegate = self; [self.view addSubview:field];
[self.view addSubview:field1];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
NSLog(@"点击事件");
} //代理方法的实现,如果返回为NO则相应的操作不被实现(例如:删除文本不起作用)
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == field)
NSLog(@"field将要编辑");
else if(textField == field1)
NSLog(@"field1将要编辑");
return YES;
}// 将要编辑。 - (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField == field)
NSLog(@"field开始编辑");
else if(textField == field1)
NSLog(@"field1开始编辑");
}// 开始编辑
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // 将要结束编辑
//- (void)textFieldDidEndEditing:(UITextField *)textField; // 结束编辑
//
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == field)
NSLog(@"field开始键入字符");
else if(textField == field1)
NSLog(@"field1开始键入字符");
return YES;
}// 开始键入字符
//
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if(textField == field)
NSLog(@"field开始删除字符");
else if(textField == field1)
NSLog(@"field1开始删除字符");
return YES;
}// 在执行field.clearButtonMode(后面有"X")起作用
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == field)
NSLog(@"field返回");
else if(textField == field1)
NSLog(@"field1返回");
[self.view endEditing:YES];
return YES;
}// 返回时调用,可以利用其退出键盘 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
1.UITextView
#import "ViewController.h" @interface ViewController ()<UITextViewDelegate>
{
UITextView *textView;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
textView = [[UITextView alloc] initWithFrame:CGRectMake(, , , )];
textView.backgroundColor = [UIColor yellowColor];
textView.editable = YES;//是否允许编辑
textView.scrollEnabled = YES;//是否允许滚动
textView.bounces = YES;//弹簧效果
textView.layer.borderColor = [UIColor redColor].CGColor;//边框颜色
textView.layer.cornerRadius = ;//圆角半径
textView.layer.borderWidth = ;//边框粗细
textView.layer.masksToBounds = YES;//设置超出主图层的部分把它给剪切掉。
//阴影设置
textView.clipsToBounds =NO;//要把其设为NO
textView.layer.shadowColor=[UIColor redColor].CGColor;
textView.layer.shadowOffset=CGSizeMake(, );
textView.layer.shadowOpacity=;
textView.layer.shadowRadius=; textView.returnKeyType = UIReturnKeyDefault;
/*
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
};
*/
textView.delegate = self;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
[self.view addSubview:textView];
}
-(BOOL)textView:(UITextView *)textView1 shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{ if([text isEqual:@"\n"])
{
[self.view endEditing:YES];
}
return YES;
} -(void)textViewDidChange:(UITextView *)textView1
{
NSLog(@"%@",textView1.text);
}//文本改变了之后
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)textViewDidBeginEditing:(UITextView *)textView1
{
NSLog(@"%@",textView1.text);
}
@end
UIScollView
#import "ViewController.h"
#define WIDE [[UIScreen mainScreen] bounds].size.width
#define HIGH [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()<UIScrollViewDelegate>
{
UIScrollView *scrollVIew;
UIImageView *imageView;
}
@end @implementation ViewController - (void)viewDidLoad {//注意: window大小 ->scrollView大小->
scrollView内容大小-> imageView大小
[super viewDidLoad];
scrollVIew = [[UIScrollView alloc]initWithFrame: CGRectMake(, , WIDE-, HIGH-)];
scrollVIew.backgroundColor= [UIColor grayColor];
scrollVIew.contentSize = CGSizeMake(*, *);// scrollVIew内容的大小,与 scrollVIew大小无关
// scrollVIew.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/8b82b9014a90f603e4fbc5573b12b31bb051ed53.jpg"]]]];
imageView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/8b82b9014a90f603e4fbc5573b12b31bb051ed53.jpg"]]]];//从网络获取图片放至 imageView
imageView.frame = CGRectMake(, , *-, *-);//设置 imageView大小,此处比scrollVIew小一圈,是以scrollVIew坐标系为基准的
[scrollVIew addSubview:imageView];//将 imageView添加到scrollVIew
scrollVIew.contentOffset = CGPointMake(, );//设置初始位置
// CGPoint point = scrollVIew.contentOffset;////获取scrollView当前滚动的位置
// scrollVIew.bounces = NO;//是否有弹簧效果
// scrollVIew.showsHorizontalScrollIndicator = NO;//是否显示水平滚动条,默认yes
// scrollVIew.showsVerticalScrollIndicator = NO;//是否显示垂直滚动条
scrollVIew.pagingEnabled = NO;//是否允许整页滚动,默认no(跳页)
scrollVIew.scrollEnabled = YES;//是否允许滚动(手滚,强制跳转可以)
scrollVIew.scrollsToTop = NO;//是否允许用户点击状态栏时,滑动到顶部,默认yes
// [scrollVIew setContentOffset:CGPointMake(100, 0) animated:YES];//设置scrollView滚动到某个位置,是否有动画效果
// [scrollVIew scrollRectToVisible:CGRectMake(375, 20, self.view.frame.size.width, self.view.frame.size.height) animated:YES];//设置scrollView滚动到某可视区域,是否添加动画效果
scrollVIew.minimumZoomScale = 0.1;//最小缩放倍数
scrollVIew.maximumZoomScale = ;//最大缩放倍数
scrollVIew.delegate = self;
[self.view addSubview:scrollVIew];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//视图是否在滚动
}
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//滚动视图开始被拖动
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//结束拖动,
}
-(void)scrollViewWillBeginDecelerating: (UIScrollView *)scrollView{
//滚动视图开始减速
}
-(void)scrollViewDidEndDecelerating: (UIScrollView *)scrollView{
//减速停止
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//实现缩放视图代理方法,不实现此方法无法缩放 //告诉scrollView,是哪个子视图要进行缩放
return imageView;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
//正在缩放
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
UIPageView
#import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate>{
UIPageControl *page;
UIScrollView *scrollView;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; page = [[UIPageControl alloc] initWithFrame:CGRectMake(, , , )];
page.backgroundColor = [UIColor clearColor];
page.numberOfPages = ;//设置页数(多少个点)
page.currentPage = ;//设置当前选中页
NSLog(@"%ld",page.currentPage);//获取当前选中页下标
page.pageIndicatorTintColor = [UIColor greenColor];//未选中颜色
page.currentPageIndicatorTintColor = [UIColor redColor];//当前选中的颜色
[page addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:page]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
scrollView.contentSize = CGSizeMake(, );
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view.backgroundColor = [UIColor grayColor];
[scrollView addSubview:view]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view1.backgroundColor = [UIColor brownColor];
[scrollView addSubview:view1]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view2.backgroundColor = [UIColor purpleColor];
[scrollView addSubview:view2]; } -(void)change:(id)page{
NSLog(@"--%zi",[page currentPage]); CGPoint p = {[page currentPage]*,};
[scrollView setContentOffset:p animated:YES];
} -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int index = scrollView.contentOffset.x/scrollView.frame.size.width;
page.currentPage = index;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
UITableView
//遵从协议<UITableViewDataSource,UITableViewDelegate>
//初始化
myTableView2 = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStylePlain];
myTableView2.delegate = self;
myTableView2.dataSource = self;
[self.view addSubview:myTableView2];
//协议的实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return talentPool.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"cell";
//尝试从可重用的cell队列中取出来(屏幕需要显示cell+1)
//如果没有取到,就新建一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];//出列可重用的细胞与标识符
if(cell == nil)
{
//实例化一个新的cell,cell有一个重用标识 @"cell"
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
}
cell.textLabel.text = [talentPool[indexPath.row] name];
cell.detailTextLabel.textColor = [UIColor redColor];
cell.detailTextLabel.text = [NSString stringWithFormat: @"学校%@ 等级%d NO:%ld",[talentPool[indexPath.row] school],[talentPool[indexPath.row] levell],indexPath.row+]; return cell;
}
//当我们点击某个cell时,会执行下面这个方法,方法中调用另一方法执行具体操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
delete = indexPath.row;
}
UITableView
//[tableVIew reloadData];//重新加载数据***********
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
long delete;
UITableView *tableVIew;
NSMutableArray *array;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
tableVIew = [[UITableView alloc]initWithFrame:self.view.bounds];
// tableVIew.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic.baike.soso.com/p/20131220/20131220045831-1022519378.jpg"]]]];
tableVIew.separatorColor = [UIColor redColor];////设置tableView分割线的颜色
// UIImageView *imageVIew = [[UIImageView alloc]initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic.baike.soso.com/p/20131220/20131220045831-1022519378.jpg"]]]];//从网络获取图片
// imageVIew.frame = CGRectMake(0, 0 ,0, 40);
// tableVIew.tableHeaderView = imageVIew;//设置列表的头视图,可以是view或者其子类的对象
// tableVIew.tableFooterView = imageVIew;//设置列表的底部视图,可以是view或者其子类的对象
tableVIew.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//设置分割线风格
/*
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,//无分割线
UITableViewCellSeparatorStyleSingleLine,//有内容处无分割线,否则有
UITableViewCellSeparatorStyleSingleLineEtched // 有分割线
};
*/
// tableVIew.rowHeight = 80;//设置行高
tableVIew.delegate = self;//设置代理
tableVIew.dataSource = self;//设置数据源,因为在实现协议方法时,用了大量的返回值,所以需要对视图控制器实现代理 [self.view addSubview:tableVIew];
array = [NSMutableArray arrayWithObjects:@"刘冠", @"刘冠", @"刘冠", @"刘冠",nil]; }
//协议方法实现
#pragma mark - UITableViewDataSourceMethod-1
//设置分组
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
// return 3;
//}
//设置每组行数(默认一组)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// if(section == 0)//第一组
// return 4;
// else if(section == 1)//第二组
// return 3;
// return 2;//第三组
return array.count;//返回数组长度
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"cell";
//尝试从可重用的cell队列中取出来(屏幕需要显示cell+1)
//如果没有取到,就新建一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];//出列可重用的细胞与标识符
if(cell == nil)
{
//实例化一个新的cell,cell有一个重用标识 @"cell"
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string];
/*
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 只显示图片和标题
UITableViewCellStyleValue1, // 显示图片,标题和子标题(子标题在右边)
UITableViewCellStyleValue2, // 标题和子标题
UITableViewCellStyleSubtitle //上下结构 显示图片,标题和子标题(子标题在下边)
}; // available in iPhone OS 3.0
*/
}
cell.backgroundColor = [UIColor clearColor];
cell.imageView //设置图片
//.tableHeaderView
cell.textLabel.text =array[indexPath.row];
77 cell.detailTextLabel.textColor = [UIColor redColor];
78 // cell.detailTextLabel.text = [NSString stringWithFormat: @"郑州科技学院 NO:%ld", indexPath.row+1];
79 //在现实中的实现方法
80 // cell.textLabel.text = [talentPool[indexPath.row] name];
81 // cell.detailTextLabel.text = [NSString stringWithFormat: @"学校%@ 等级%d NO:%ld",[talentPool[indexPath.row] school],[talentPool[indexPath.row] levell],indexPath.row+1];
82 /**************************************/
83 // cell.accessoryType/*配件*/= UITableViewCellAccessoryDetailDisclosureButton;//右侧标识图
84 cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bug"]];//自定义右侧标识视图
85 cell.selectionStyle = UITableViewCellSelectionStyleBlue;//设置cell被选中的颜色
86 cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
87 UIView *view = [[UIView alloc] initWithFrame:cell.frame];
88 view.backgroundColor = [UIColor blueColor];
89 cell.selectedBackgroundView = view;//自定义cell选中背景
90 return cell;
91 }
92 //设置每组头部的标题
93 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
94 if(section == )//第一组
95 return @"第一组";
96 else if(section == )//第二组
97 return @"第二组";
98 return @"第三组";//第三组
99 }
//设置每组底部标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"END";
}
//设置tableView是否可编辑,默认YES,向左滑动出现删除按钮
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//对tableView进行删除.....操作与上一个方法配合使用
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // tableVIew
NSLog(@"删除%ld",indexPath.row);
[array removeObjectAtIndex:indexPath.row];
[tableVIew deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}
//左侧索引显示信息,返回数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return ;
}
//设置每组底部视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return ;
}
// 设置每组头部视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
}
//自定义组头部视图,会覆盖数据源中的设置头部标题方法
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return nil;
}
//自定义组底部视图,会覆盖数据源中的设置底部标题方法
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return nil;
}
//tableview行被点击,通过indexPath.row判断点击的是哪一行
//当我们点击某个cell时,会执行下面这个方法,方法中调用另一方法执行具体操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
delete = indexPath.row;
[tableVIew deselectRowAtIndexPath:indexPath animated:YES];//取消被选中
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end
UIPageController
UIPageControl
#import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate>{ UIScrollView *s;
UIPageControl *p;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
p = [[UIPageControl alloc]initWithFrame:CGRectMake(, , , )];//初始化创建
p.numberOfPages = ;//设置页数
p.currentPage = ;//设置当前页数
p.backgroundColor = [UIColor clearColor];
p.pageIndicatorTintColor = [UIColor orangeColor];//未选中颜色
p.currentPageIndicatorTintColor = [UIColor greenColor];//当前选中颜色
[p addTarget:self action:@selector(pag) forControlEvents:UIControlEventValueChanged];//valuechange!!!!
[self.view addSubview:p];
s = [[UIScrollView alloc]initWithFrame:CGRectMake(, , , )];
s.contentSize = CGSizeMake(,);
s.delegate = self;
s.pagingEnabled = YES;
[self.view addSubview:s];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5.jpg"]];
[s addSubview:v];
UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"6.jpg"]];
[s addSubview:v1];
UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"9.jpg"]]; [s addSubview:v2];
[self.view addSubview:p];
}
-(void)pag{
CGPoint p1 = {[p currentPage]*,};//结构体
[s setContentOffset:p1 animated:YES];//p->s page到scroll
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{//s->page
CGPoint p1 = s.contentOffset;
int index = p1.x/s.frame.size.width;
p.currentPage = index; }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end 缩放
#import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate>{ UIScrollView *s;
UIImageView *i; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
s = [[UIScrollView alloc]initWithFrame:self.view.bounds];
s.delegate = self;
s.userInteractionEnabled = YES;//这里不需要交互
s.minimumZoomScale = ;
s.maximumZoomScale = ;
[self.view addSubview:s];
i = [[UIImageView alloc] initWithFrame:CGRectMake(, , ,)];
i.image = [UIImage imageNamed:@"4.jpg"];
[s addSubview:i];
}
//告诉scrollView,是哪个子视图要进行缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return i;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{ }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
手势识别(非自己总结,有待验证)
两个手势(.缩放手势 .拖拽手势)
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UIImageView *v = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];
v.frame = self.view.bounds;
UITapGestureRecognizer *t =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(image:)];//添加手势
t.numberOfTapsRequired = ;//点击次数
t.numberOfTouchesRequired = ;//需要点击的手指个数
v.tag = ;
v .userInteractionEnabled = YES;
[v addGestureRecognizer:t];
//拖动手势
UIPanGestureRecognizer *p = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];//添加拖动手势
[v addGestureRecognizer:p];
[self.view addSubview:v]; }
-(void)image:(UITapGestureRecognizer *)ges{
UIView *vi = ges.view;
NSLog(@"*****%zi",vi.tag);
}
-(void)pan:(UIPanGestureRecognizer *)pa{
NSLog(@"%zi",pa.state);//打印拖动时对象所处的状态
CGPoint point = [pa locationInView:self.view]; //获取拖动手势的开始拖动点的坐标
pa.view.center = point; //把开始拖动点的坐标赋值给pa所在的图片的中点坐标(因为pa是一个手势,是可变的,所以中心点会跟着手势变,产生拖动效果)
}
边框覆盖法解决UISegmentedControl不能隐藏边框问题//在ScrollVIew上面放置分段控制器
NSArray * topArray = @[@"国内",@"国际",@"时尚",@"明星",@"淘宝",@"天气",@"健康",@"生活",@"历史",@"人文"];
// [[UISegmentedControl appearance] setTitleTextAttributes:@{
// NSForegroundColorAttributeName : [UIColor redColor]
// } forState:UIControlStateNormal];
UISegmentedControl *segmented = [[UISegmentedControl alloc]initWithItems:topArray];
segmented.frame = CGRectMake(0, 5, 70*10, 40);
segmented.tintColor = [UIColor redColor];//边框和选中颜色
[top addSubview:segmented];
[self.view addSubview:top];
NSArray * topArray1 = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
UISegmentedControl *segmented1 = [[UISegmentedControl alloc]initWithItems:topArray1];
segmented1.frame = CGRectMake(0, 5, 70*10, 40);
[segmented1 setTintColor:[UIColor whiteColor]];//边框和选中颜色
[segmented1 setUserInteractionEnabled:NO];
[top addSubview:segmented1];
[self.view addSubview:top];
UI学习之常用方法的更多相关文章
- UI学习之常用方法(续)
UIView 1. UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(100,100, 100, 30)]; [vv.layer setB ...
- IOS开发-UI学习-sqlite数据库的操作
IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...
- 适合新手小白的UI学习路线完整版
UI设计是很多年轻人活着想转行学习的人的新职业目标,越来越多的人看到UI设计良好的就业发展前景,纷纷投入到UI设计的大军中来,想学习UI设计,很多小白并不知道从何开始学起,用什么样的方法去学习,所以今 ...
- Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)
Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...
- 36.Android之多线程和handle更新UI学习
android经常用到多线程更新UI,今天学习下. 首先布局比较简单: <?xml version="1.0" encoding="utf-8"?> ...
- Android UI学习组件概述
Android的UI组件繁多,如果学习的时候不能自己总结和分类而是学一个记一个不去思考和学习他们内在的联系那真的是只有做Farmer的命了.为了向注定成为Farmer的命运抗争,在学习Android的 ...
- 个人技术博客Alpha----Android Studio UI学习
项目联系 这次的项目我在前端组,负责UI,下面简略讲下学到的内容和使用AS过程中遇到的一些问题及其解决方法. 常见UI控件的使用 1.TextView 在TextView中,首先用android:id ...
- 2019版UI学习路线(含大纲+视频+工具+网盘+面试题)
2019最新UI设计师教程(学习路线+课程大纲+视频教程+面试题+学习工具) 什么是全链路UI设计 UI设计师是随着网络而兴起的新兴设计行业,从事对软件的人机交互.操作逻辑.界面美观的整体设计工作.涉 ...
- Semantic ui 学习笔记 持续更新
这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...
随机推荐
- LaTeX常用数学符号表示方法
转自:http://www.mohu.org/info/symbols/symbols.htm 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LATEX2e 介绍”) 1. ...
- java泛型中的对象
import java.util.HashMap; class Key { String s; Key(String s) { this.s = new String(s); } @Override ...
- redis HA高可用方案Sentinel和shard
1.搭建redis-master.redis-slave以及seninel哨兵监控 在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的” ...
- C# Label显示多行文本及换行(WinForm/WebForm)
转载: http://www.cnblogs.com/greatverve/archive/2011/10/20/label-multi.html WinForm 很多朋友都会在开发WinForm中遇 ...
- C#高级特性_Attribute
Attribute: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microsoft .NET ...
- you-get中文说明
来源于:https://github.com/soimort/you-get/wiki/%E4%B8%AD%E6%96%87%E8%AF%B4%E6%98%8E You-Get 乃一小小哒命令行程序, ...
- [转]Spring——jar包详解
原文地址:http://my.oschina.net/huhaoren/blog/300856?p=1 spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring ...
- Swift开发小技巧--扫描二维码,二维码的描边与锁定,设置扫描范围,二维码的生成(高清,无码,你懂得!)
二维码的扫描,二维码的锁定与描边,二维码的扫描范围,二维码的生成(高清,无码,你懂得!),识别相册中的二维码 扫描二维码用到的三个重要对象的关系,如图: 1.懒加载各种类 // MARK: - 懒加载 ...
- ActiveMQ_点对点队列(二)
一.本文章包含的内容 1.列举了ActiveMQ中通过Queue方式发送.消费队列的代码(普通文本.json/xml字符串.对象数据) 2.spring+activemq方式 二.配置信息 1 ...
- python 进程间共享数据 (一)
def worker(num, mystr, arr): num.value *= 2 mystr.value = "ok" for i in range(len(arr)): a ...