编程技巧 - 4
来源:互联网 发布:echo gumi动作数据 编辑:程序博客网 时间:2024/06/09 17:33
26: 私有扩展 + 嵌套:
外露的两个方法接口:
#import <Foundation/Foundation.h>#import <AudioToolbox/AudioToolbox.h>@interface JSMessageSoundEffect : NSObject+ (void)playMessageReceivedSound;+ (void)playMessageSentSound;@end
用私有扩展“保护”的一个私有方法,并且你发现这样的嵌套会顺带保护它们的封装性:
#import "JSMessageSoundEffect.h"@interface JSMessageSoundEffect ()//私有扩展,就是不想这个方法暴露,并且下面两个赋参的方法也是私有,安全的+ (void)playSoundWithName:(NSString *)name type:(NSString *)type;@end@implementation JSMessageSoundEffect+ (void)playSoundWithName:(NSString *)name type:(NSString *)type{ NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type]; if([[NSFileManager defaultManager] fileExistsAtPath:path]) { SystemSoundID sound; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &sound); AudioServicesPlaySystemSound(sound); } else { NSLog(@"Error: audio file not found at path: %@", path); }}+ (void)playMessageReceivedSound{ [JSMessageSoundEffect playSoundWithName:@"messageReceived" type:@"aiff"];}+ (void)playMessageSentSound{ [JSMessageSoundEffect playSoundWithName:@"messageSent" type:@"aiff"];}@end
27: 更简洁地取宽高
CGSize size = self.view.frame.size; CGRect tableFrame = CGRectMake(0.0f, 40.0f, size.width, size.height - 40 - INPUT_HEIGHT);
比你直接来两个 width 和 height 强得多啦。
28.取各种尺寸
/** @brief frame 左上角x view.frame.origin.x */#define VIEW_TLX(view) (view.frame.origin.x)/** @brief view frame 左上角y view.frame.origin.y */#define VIEW_TLY(view) (view.frame.origin.y)/** @brief view frame 右下角x */#define VIEW_BRX(view) (view.frame.origin.x + view.frame.size.width)/** @brief view frame 右下角y */#define VIEW_BRY(view) (view.frame.origin.y + view.frame.size.height)/** @brief view frame width */#define VIEW_W(view) (view.bounds.size.width)/** @brief view frame height */#define VIEW_H(view) (view.bounds.size.height)/** @brief CGRect 左上角x */#define FRAME_TLX(frame) (frame.origin.x)/** @brief CGRect 左上角y */#define FRAME_TLY(frame) (frame.origin.y)/** @brief CGRect width */#define FRAME_W(frame) (((frame).size).width)/** @brief CGRect height */#define FRAME_H(frame) (((frame).size).height)
分清楚Frame 和 Bounds的区别就可以明白 左上右上 的原理了。
29.优雅取消计时器
if (_scrollTimer != nil) { [_scrollTimer invalidate]; _scrollTimer = nil; }
30.取消键盘一个巧做法:
@property (nonatomic, strong) id currentTextField; //关闭键盘
#pragma mark - UITextFieldDelegate- (void)textFieldDidBeginEditing:(UITextField *)textField{ _currentTextField = textField;}
在适当位置关闭键盘:
[_currentTextField resignFirstResponder];//适当地方
31.尽量优雅地缩短你的代码!例如直接地判断返回布尔值!
self.inputToolBarView.sendButton.enabled = ([textView.text trimWhitespace].length > 0);
32.尽量贴近苹果公司的处理方法,无所不用其技
贴一个button的set方法,实现思想和底层的先release再retain非常非常相像:
#pragma mark - Setters- (void)setSendButton:(UIButton *)btn{ if(sendButton) [sendButton removeFromSuperview]; sendButton = btn; [self addSubview:self.sendButton];}
0 0
- 编程技巧 - 4
- c#4个编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- Android 百分比布局支持库使用
- MSCcomm控件出现ASSERT错误
- 任意匹配
- 3_寻找假币问题(分治法)
- javascript生成器函数function generator初探和理解
- 编程技巧 - 4
- scala学习之:scala中的单例
- 专业之外——活动负责人的体验
- linq实现随机查询
- Java回调机制解析
- C语言位运算详解
- 色相(H)饱和度(S)明度(L)与RGB的转换以及在android上的试验
- Container With Most Water
- 设计模式(8):中介者模式