标签:uiview frame bounds transform
1、UIView的常见的属性
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
@property(nonatomic) CGAffineTransform transform;
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
补充:
// 视图的内容模式
@property(nonatomic) UIViewContentMode contentMode;
一般选作:UIViewContentModeCenter,图片的大小保持原来的size
* frame和bounds,center的区别:*
* frame:以父控件的左上角为坐标原点,可以确定控件的位置(origin)和大小(size)
* bounds:以自己的左上角为坐标原点,可以确定控件的大小(size)
* center:可以确定控件的位置
* 注意:OC不允许修改对象结构体属性成员
2、transform的属性
利用transform可以修改控件的位置缩放旋转
1> 创建一个transform属性
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty) ;
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
2> 在transform的基础上进行叠加
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);
CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);
3> 清空之前设置的transform属性
view.transform = CGAffineTransformIdentity;
补充:坐标转换
CGPoint *newpoint = [view1 convertPoint:<#(CGPoint)#> toView:view2];
坐标从view1转换到view2
//CGRect *newRect = [view1 convertPoint:<#(CGPoint)#> fromView:view2];
//坐标从view2转换到view1
CGPoint是frame前面是subview
CGPoint是bounds前面是自身的view
3.UIView的常见方法
- (void)addSubview:(UIView*)view;
- (void)removeFromSuperView;
- (UIView*)viewWithTag:(NSInteger)tag;
1> 加餐(监听添加子控件的过程)
// 添加子控件
- (void)didAddSubview:(UIView *)subview;
// 将要移除子控件
- (void)willRemoveSubview:(UIView *)subview;
// 将要添加到父控件
- (void)willMoveToSuperview:(UIView *)newSuperview;
// 添加到控件
- (void)didMoveToSuperview;
4.UIView实现简单动画
两种方式实现动画
1> 头尾式
[UIView beginAnimations:nil context:nil];
// 需要执行的动画
[UIView commitAnimations];
2> Block式
[UIView animateWithDuration:0.5 animations:^{
// 需要执行的动画的代码
}];
**
**
1> normal--> UIControlStateNormal
2> highlighted-->UIControlStateHighlighted
3> selected-->UIControlStateSelected
4> disabled-->UIControlStateDisabled
1.常见属性
@property(nonatomic,readonly,retain) NSString *currentTitle;
@property(nonatomic,readonly,retain) UIColor *currentTitleColor;
@property(nonatomic,readonly,retain) UIImage *currentImage;
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage;
2.常见属性设置
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
btn.titleLabel.font = [UIFont systemFontOfSize:13];
3.属性的获得
- (NSString *)titleForState:(UIControlState)state;
- (UIColor *)titleColorForState:(UIControlState)state;
- (UIImage *)imageForState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;
4.手动创建UIButton的代码
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forstate:UIControlStateNormal];
[btn setTitle:@"点我啊" forstate:UIControlStateNormal];
[btn settitleColor:[UIColor redColor] forstate:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIcontrolEventTouchUpInside];
5.UIControl 控件的布局
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
1.帧动画相关属性和方法
1> 属性
// 序列帧图片数组
@property(nonatomic,copy) NSArray *animationImages;
// 帧动画持续的时间
@property(nonatomic)NSTimeInterval animationDuration;
// 帧动画执行的次数
@property(nonatomic)NSInteger animationRepeatCount;
2> 方法
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
2.UIImagede的2种加载方式:
1> 有缓存(程序所占的内存会一直停留在程序中)
+ (UIImage*)imageNamed:(NSString*)name;
2> 无缓存(图片所占内存会在一些特定的操作后被清除)
+ (UIImage*)imageWithContentsOfFile:(NSString*)path;
- (id)initWithContentsOfFile:(NSString*)path;
补充:
// 渲染模式,可以更改原图片的颜色
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
1.属性
text font textColor textAlignment
2.UIFont的设置方法
// 系统默认的字体
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
// 粗体
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
// 斜体
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;
控制器UIViewController遵守UIAlertView的代理协议
<UIAlertViewDelegate>
1.创建
[[[UIAlertView alloc] initWithTitle:@"标题" message:@"消息" delegate:self cancelButtonTitle:@"取消按钮的标题" otherButtonTitles:@"其他按钮的标题1",@"其他按钮标题2" nil] show];
2.实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{}
1.控制器UIViewController遵守UIActionSheet的代理协议
<UIActionSheetDelegate>
2.创建
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
3.展示
[sheet showInView:self.view];
4.实现代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{}
1.通过UITextField的代理方法能够监听键盘最右下角按钮的点击
1> 成为UITextField的代理
self.textField.delegate = self;
2> 遵守UITextFieldDelegate协议,实现代理方法
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
3> 在UITextField的左边放一个view(用于键盘的输入框)
self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,8,0)];
self.textField.leftViewMode = UITextFieldViewModeAlways;
2.通过监听UITextField来判断文本框是否有输入
// 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField];
// 监听到有输入时执行的方法
- (void)textChange
{
}
// 移除监听器
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserve:self];
}
3.通过添加事件(UITextField也是继承自UIControl的)
[self.accountField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
1.键盘退去
1> 注销响应者
[self.textField resignFirstResponder];
2> 整个控制器的view都不能进行键盘输入
[self.view endEditing:YES];
2.键盘弹出
[self.textField becomeFirstResponder];
1.JPG和PNG图片格式的比较
JPG:压缩比比较高,通常用于照片,网页,属于有损压缩,解压时,对CPU的消耗大
PNG:压缩比较高,无损压缩,解压效率高,推荐使用
2.weak和strong的使用
1> 为什么UI控件使用weak:
UI控件要添加到UIViewController的_view属性中(addSubview:方法),其中addSubview:操作就是强引用
所以没有必要在UIViewController属性中把UI控件再次设置为strong强引用了
2> delegate代理为什么是weak:
UIViewController控制器中强引用的UI控件,它的代理一般设置为控制器本身UIViewController,如果把delegate代理也设置为strong 就是循环引用了,会造成内存泄露。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:uiview frame bounds transform
原文地址:http://blog.csdn.net/wangbo_neuq/article/details/48081003