拾遗系列(五)Tips

来源:互联网 发布:深圳知金教育怎么样 编辑:程序博客网 时间:2024/06/03 16:43

图片拉伸

//加载原图UIImage *image = [UIImage imageNamed:@""];//拉伸处理(说明需要保护的区域)image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30) resizingMode:UIImageResizingModeStretch];
UIButton *btn;//按钮内边距btn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);btn.imageEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);btn.titleEdgeInsets = UIEdgeInsetsMake(30, 30, 30, 30);  UIButton的content中包含imageView、label。上面Insets即对于每个View设置。

对于UITextField没有UIButton的EdgeInsets属性。若想设置UITextField的编辑区域离UITextField左侧有一段距离,可通过下面方式来实现。

UITextField *textField;UIView *leftView = [[UIView alloc]init];leftView.frame = CGRectMake(0, 0, 10, 10);textField.leftView = leftView;textField.leftViewMode = UITextFieldViewModeAlways;

通知中心(NSNotificationCenter)

  • 每个应用程序都有一个通知中心,专门负责协助不同对象之间的消息通讯
  • 任何一个对象都可以向通知中心发布通知,描述自己在做什么。其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或某个特定的对象发布通知时)收到这个通知
//发送通知NSNotification *note = [NSNotification notificationWithName:@"what happened" object:self userInfo:@{@"time":@"2012",@"desc":@"earth dead"}];[[NSNotificationCenter defaultCenter]postNotification:note];
//接收通知[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"what happened" object:nil];//object == nil 表示接收任何对象发出的“what happened”//接收到消息之后的处理-(void)receiveNotification:(NSNotification *)note {}

系统自带的一些通知

UIDevice通知

UIDeviceOrientationDidChangeNotification //设备旋转UIDeviceBatteryStateDidChangeNotification//电池状态改变UIDeviceBatteryLevelDidChangeNotification//电池电量改变UIDeviceProximityStateDidChangeNotification//近距离传感器(如设备贴近使用者脸部)

键盘通知

UIKeyboardDidShowNotification//键盘即将显示UIKeyboardWillHideNotification//键盘即将隐藏 UIKeyboardWillChangeFrameNotification//键盘frame改变

获取到系统通知时,可查看系统带的note

NSLog(@"%@",note.userInfo);//可以查看到系统传输的note{UIKeyboardAnimationCurveUserInfoKey = 7;UIKeyboardAnimationDurationUserInfoKey = "0.25";UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 253}}";UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 694.5}";UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 441.5}";UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 253}}";UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 315}, {320, 253}}";UIKeyboardIsLocalUserInfoKey = 1;}

移除通知

- (void)dealloc {[[NSNotificationCenter defaultCenter]removeObserver:self];}

Transform 形变属性

清空,回到初始

self.view.transform = CGAffineTransformIdentity;

自定义按钮(UIButton)的内部结构

按钮中包含imageView、title
重新设置位置

  • 1
//重新设置Image位置- (CGRect)imageRectForContentRect:(CGRect)contentRect {}//重新设置title位置-(CGRect)titleRectForContentRect:(CGRect)contentRect {}
  • 2 推荐使用此方法
- (void)layoutSubviews {    [super layoutSubviews];    //    self.imageView.frame = CGRectMake(0, 0, 10, 10);    self.titleLabel.frame = CGRectMake(0, 10, 10, 10);}

代理设计模式

  • 作用 (两个对象之间传递消息)
    • A对象监听B对象的一些行为,A成为B的代理
    • B对象想告诉A对象一些事情,A成为B的代理
  • 开发步骤
    • 拟一份协议 (协议名字的格式:控件名+Delegate),在协议里面声明一些代理方法(一般代理方法都是@optional)
    • 声明一个代理属性:@property(nonatomic,weak)id<代理协议> delegate
    • 在内部发生某些行为时,调用代理对应的代理方法,通知代理内部发生的事情
    • 设置代理:xxx.delegate = yyy;
    • yyy对象遵守协议,实现代理方法
  • 总结
    如果想监听别人一些行为,那么你就要成为别人的代理

代理与通知的与区别

  • 代理:1个对象只能告诉另一个对象发生什么事
  • 通知:1个对象可以告诉N个对象发生什么事

KVO (也可以监听)、KVC

  • KVO:Key Value Observing 常见作用:用来监听属性值改变
  • KVC: Key Value Coding 常见作用:给模型属性赋值
[self.tableView addObserver:self forKeyPath:@"numberOfSections" options:kNilOptions context:nil];//KVO监听方法- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {}//移除监听-(void)dealloc {    [self.tableView removeObserver:self forKeyPath:@"numberOfSections"];}

自定义键盘

UITextField *field = [[UITextField alloc]init];//自定义键盘field.inputView = [[UIView alloc]init];

PCH文件

里面内容都是共享的,每个文件都会共享。但需要配置,通过 Build Settings –> prefix
- 存放一些公共宏
- 存放公用头文件
- 自定义Log

//宏里面可变参数:...//函数里面可变参数:__VA_ARGS__#define SDHLog(...) NSLog(__VA_ARGS__)#ifdef DEBUG//调试阶段#define SDHLog(...) NSLog(__VA_ARGS__)#else#define SDHLog(...)#endif

OC文件会定义 OBJC
OC头文件中

0 0
原创粉丝点击