UI控件用法


 label

label.backgroundColor = [UIColor redColor];
label.text = @"hello";
label.textColor = [UIColor yellowColor]
label.font = [UIFont systemFontOfSize:30];
label.textAlignment = NSTextAlignmentCenter;
label.enabled = NO;//是否可用(可编辑)
label.hidden = NO//是否隐藏
label.numberOfLines = 1;//设置文本显示行数,默认值1,0任意行
//文字的截断方式
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
//根据label的宽度自适应文本的大小
label.adjustsFontSizeToFitWidth = YES;
设置最小缩放因子
label.minimumScaleFactor = 0.9;
label.baselineAdjustment = UIBaselineAdjustmentNone;
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(-5, -5);

CALayer 图层

view.clipsToBounds = YES;裁剪超出父视图分
label.layer.cornerRadius = 20;
label.layer.masksToBounds = YES;
btn.layer.cornerRadius = 50; //设置圆角
btn.layer.borderWidth = 1; //设置边框
btn.layer.borderColor = [UIColor blackColor].CGColor;

UIButton

[btn currentTitle]
btn.showsTouchWhenHighlighted = NO;
    button上 图像文字的处理方法
CGFloat btnWidth = CGRectGetWidth(btn.frame);
CGFloat btnHeight = CGRectGetHeight(btn.frame);
CGFloat imageWidth = CGRectGetWidth(btn.imageView.frame);
//调整图像和文字的位置
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.imageEdgeInsets = UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) / 2, (btnWidth - imageWidth) / 2, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake((btnHeight - imageHeight - labelHeight) / 2 + imageHeight, (btnWidth - lableWidth) / 2 - imageWidth, 0, 0);

 UIView

//修改bounds属性的左上角的位置坐标,不会影响视图相对父视图的位置
CGRect bounds = view.bounds;
bounds.origin.x = 50;
bounds.origin.y = 50;
bounds.size.height = 100;
view.bounds = bounds;
 transform//变形
view.transform = CGAffineTransformMakeTranslation(-30, 30);平移
view.transform = CGAffineTransformMakeScale(1, 2);进行缩放,会覆盖原来的值
中心点为轴,顺时针旋转 参数表示弧度,M_PI * 旋转的角度 / 180
view.transform = CGAffineTransformMakeRotation(M_PI * 45 / 180);
//形变的叠加效果
//先旋转,再缩放
label.transform = CGAffineTransformRotate(label.transform, M_PI * 30 / 180)

//获取某个视图所有的子视图
    NSArray *views = superView.subviews;
 //获取某个视图的父视图
UIView *v = superView.superview;
//判断某个视图是否是另一视图的子视图
BOOL ret = [superView isDescendantOfView:self.view];
//从父视图删除某一子视图
[subView1 removeFromSuperview];
//裁剪子视图超过父视图边界的部分
superView.clipsToBounds = YES;
//将一个视图放到最后
[self.view sendSubviewToBack:yellowView];
[self.view bringSubviewToFront:redView];
//根据索引交换两个视图的位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
[self.view insertSubview:redView aboveSubview:blueView]

     UIView动画

//参数1:动画的名称
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];设置动画持续时间
[UIView setAnimationRepeatCount:3]动画重复执行次数
[UIView setAnimationDelay:2];/设置动画开始的延时
[UIView setAnimationRepeatAutoreverses:YES];一次动画结束后,是否反向执行动画
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];/设置动画执行的曲线
[UIView commitAnimations]提交动画,动画结束

usingSpringWithDamping 弹簧动画的效果,值(0-1.0)越小,效果越明显
initialSpringVelocity 初始速度,值越小,速度越快
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);


    UIImageView 动画

imageView.animationImages = mArr;
imageView.animationDuration = 0.3
imageView.animationRepeatCount = 0;
[imageView startAnimating];
imageView.isAnimating;
[imageView stopAnimating]

UITextField

//设置边框的样式
tf.borderStyle = UITextBorderStyleNone;
tf.text = @"hello";//设置文本内容
tf.placeholder = @"请输入姓名"
tf.font = [UIFont systemFontOfSize:30];//设置字体
tf.clearButtonMode = UITextFieldViewModeAlways;//设置清除按钮的样式
tf.clearsOnBeginEditing = YES;//开始编辑时,是否清空内容
[tf becomeFirstResponder];//设置为第一响应者,会自动弹出虚拟键盘
tf.isFirstResponder判断是否是第一响应者
[tf resignFirstResponder]取消第一响应者
tf.secureTextEntry = YES;是否采用安全输入方式
tf.background = [UIImage imageNamed:@"bg"];设置背景图像
tf.leftView = leftView;设置左视图
tf.leftViewMode = UITextFieldViewModeAlways;设置左视图显示样式
tf.rightView = rightView;
tf.rightViewMode = UITextFieldViewModeAlways;
    // 键盘设置
tf.keyboardAppearance = UIKeyboardAppearanceDefault;键盘的外观
tf.keyboardType = UIKeyboardTypeURL;设置键盘的类型
 tf.returnKeyType = UIReturnKeyGo;return按键的类型
二级键盘设置
tf.inputAccessoryView = accessoryView;


UINavigationController


点按隐藏或显示导航栏
[self.navigationController setNavigationBarHidden:hidden animated:YES];
nc.navigationBar.translucent = YES;YES,透明,子视图的位置相对于屏幕的上边;NO,不透明,子视图的位置相对于navaigationbar的下边
nc.navigationBar.barTintColor = [UIColor brownColor];设置naviagationbar的颜色
//设置navigationbar的前景色,不会改变navigationbar上title的颜色
nc.navigationBar.tintColor = [UIColor whiteColor];
//设置navigationbar标题的属性
[nc.navigationBar setTitleTextAttributes:@{
        NSForegroundColorAttributeName:[UIColor whiteColor],
        NSFontAttributeName:[UIFont systemFontOfSize:25]
        }];
    navigationBar
//是否隐藏navigationbar
nc.navigationBarHidden = NO;
如果图片高度是44,状态栏显示黑色;如果图片不是44,图像会自动处理,占满64的高度
[nc.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg-32"] forBarMetrics:UIBarMetricsDefault];
//返回指定的视图控制器
//viewControllers 返回导航控制器中维护的所有视图控制器
[self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];

每个视图控制器都有自己的navigationitem属性,每个视图控制器的navigationitem是不同的,通过它可以设置navigationbar的返回按钮、左右视图、titleView等属性
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;返回按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];系统风格
self.navigationItem.leftBarButtonItem = leftItem;
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];自定义
self.navigationItem.rightBarButtonItems = @[itme1, itme2, itme3, itme4, itme5];设置一组按钮
self.navigationItem.title = @"Customer";//设置标题
    
    toolbar   
//默认不显示
nc.toolbarHidden = NO;
nc.toolbar.barTintColor = [UIColor purpleColor];
//设置toolbar的风格
nc.toolbar.barStyle = UIBarStyleBlack;
self.toolbarItems = @[itme1, itme5, itme2, itme3, itme4];

    UITabBarController


//设置tabbar的颜色 tabbar高度49
tc.tabBar.barTintColor = [UIColor lightGrayColor];
//设置tabbar的前景色
tc.tabBar.tintColor = [UIColor blackColor];
tc.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_bg"];背景图像
设置选中某项时的指示图像
tc.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_s"];
tc.selectedIndex = selectIndex//设置选中的tabbar上按钮的索引
通过tabBarItem设置tabbar上显示的按钮的文字和图像    
tbc.viewControllers = @[mnc, mnc2, vc3, vc4, vc5, vc6];
NSLog(@"%@", tbc.tabBar.subviews);

ViewController3 *vc3 = [[ViewController3 alloc] init];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"vc3" image:[UIImage imageNamed:@"tab_1"] selectedImage:[UIImage imageNamed:@"tab_2"]];
    vc3.tabBarItem = item2;
    vc3.tabBarItem.tag = 12;
当跳转到某个视图控制器(入栈)时,隐藏底部的tabbar
vc11.hidesBottomBarWhenPushed = YES;

    Responder:
nextResponder/becomeFirstResponder/
resignFirstResponder/isFirstResponder

    触摸事件:Touch
UITouch *touch = [touches anyObject];
multipleTouchEnabled 多点触控
tapCount短时间内的点击次数
count触摸点的个数allTouches.count
timestamp时间戳

    手势

scale/state/rotation/direction
     tap    
numberOfTouchesRequired触摸点个数
numberOfTapsRequired点按次数
addGestureRecognizer:添加点按手势
requireGestureRecognizerToFail:当doubleTap失败的时候,使用tap
minimumPressDuration
Swipe
注意一个轻扫手势只能控制一个方向,默认向右,通过direction进行方向控制
swipeGestureToLeft.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizerDirectionRight,
UISwipeGestureRecognizerDirectionLeft,
UISwipeGestureRecognizerDirectionUp,
UISwipeGestureRecognizerDirectionDown
pan
由于拖动距离存在累加效果,所以每改变一次位置,需要对移动距离清0
setTranslation:CGPointZero inView:
pinch
scale缩放的比例    为了防止缩放过程中出现累加的效果,一次缩放后,将scale设置为1
CGAffineTransformScale(,,,)
rotation存在叠加效果,每次旋转后,需要清零

//文件存放在手机的本地磁盘上,获取文件的url对象时,需要使用
fileURLWithPath:
//声音标示
SystemSoundID
//AudioServicesCreateSystemSoundID 等方法属于Core Foundation框架,该框架是基于C语言的
 //创建音效对象
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
    //播放音效
AudioServicesPlayAlertSound(soundID);

    UIScrollView

contentSize内容的尺寸
设置内容的偏移量,contentOffset参照contentSize的坐标系
contentOffset
bounces是否回弹
contentInset设置内容的边距
scrollEnabled设置是否可以滚动
scrollsToTop是否可以滚动到内容的顶部
pagingEnabled按页滚动
显示水平和垂直方向的指示器
showsHorizontalScrollIndicator
showsVerticalScrollIndicator
indicatorStyle设置指示器的样式
minimumZoomScale最小和最大缩放比例
maximumZoomScale
zoomScale当前的缩放比例
subviews[0]

    UIPageControl

numberOfPages设置页数
currentPage设置当前页
backgroundColor
pageIndicatorTintColor未选中的点的颜色
currentPageIndicatorTintColor选中的点的颜色
isDragging判断是否正在拖动
CGRectInset(,,)以原来矩形的中心点为基准,对原矩形进行放大或缩小
showInView:展示内容

    UITableView

rowHeight行高(默认高度44)
backgroundView设置背景视图
backgroundColor
sectionHeaderHeight设置组头和组尾的高度
sectionFooterHeight
tableHeaderView/tableFooterView设置表头 尾视图
separatorColor/separatorStyl设置行间分割线的颜色
allowsSelection是否支持选中cell
allowsMultipleSelection 是否支持选中多个cell
deselectRowAtIndexPath:取消选中某行
sectionIndexBackgroundColor组索引的背景色
sectionIndexColor组索引的颜色
sectionIndexTrackingBackgroundColor设置拖动组索引时的背景色

    UITableViewCell
textLabel.
dequeueReusableCellWithIdentifier:cell的复用
accessoryType


    UICollectionViewFlowLayout与UICollectionView


//创建线性布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置item的大小
layout.itemSize = CGSizeMake(100, 150);
//行中,item的最小间距
layout.minimumInteritemSpacing = 10;
//行与行之间的最小间距
layout.minimumLineSpacing = 10;
//设置每组内容的边距
layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
//滚动方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionViewScrollDirectionVertical
//设置组头和组尾的大小
//水平方向滚动时,宽度起作用;垂直方向滚动时,高度起作用
layout.headerReferenceSize = CGSizeMake(50, 100);
layout.footerReferenceSize = CGSizeMake(50, 100);
//显示组头和组尾
layout.sectionHeadersPinToVisibleBounds = YES;
layout.sectionFootersPinToVisibleBounds = YES;

//通过布局对象,创建collectionview对象
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
//注册collectionview的cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//注册附加视图(组头或组尾)
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerID"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerID"];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值