1. 去掉tableView分割線的多余像素
首先在viewDidLoad方法加入以下代碼:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然后重寫willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
2. 簡(jiǎn)單的獲取當(dāng)前時(shí)間
// CFAbsoluteTime其實(shí)就是double
CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();
3.程序直接退出
exit(0);
4.超出父視圖范圍的控件部分響應(yīng)事件
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView* hitView = [super hitTest:point withEvent:event];
if (!hitView) {
CGPoint tempPoint = [_testBtn convertPoint:point fromView:self];
if (CGRectContainsPoint(_testBtn.bounds, tempPoint)) {
hitView = _testBtn;
}
}
return hitView;
}
5.讓一個(gè)視圖始終在最前面
view.layer.zPosition = 999;
6.判斷一個(gè)view是不是指定view的子視圖
BOOL isChildView = [childView isDescendantOfView:parentView];
7. UIViewController中的幾個(gè)重要方法
* alloc 創(chuàng)建對(duì)象,分配空間
* init (initWithNibName) 初始化對(duì)象,初始化數(shù)據(jù)
* loadView 從nib載入視圖 ,除非你沒有使用xib文件創(chuàng)建視圖
* viewDidLoad 載入完成,可以進(jìn)行自定義數(shù)據(jù)以及動(dòng)態(tài)創(chuàng)建其他控件
* viewWillAppear視圖將出現(xiàn)在屏幕之前,馬上這個(gè)視圖就會(huì)被展現(xiàn)在屏幕上了
* viewDidAppear 視圖已在屏幕上渲染完成
* viewWillDisappear 視圖將被從屏幕上移除之前執(zhí)行
* viewDidDisappear 視圖已經(jīng)被從屏幕上移除,用戶看不到這個(gè)視圖了
* dealloc 視圖被銷毀,此處需要對(duì)你在init和viewDidLoad中創(chuàng)建的對(duì)象進(jìn)行釋放.
* viewVillUnload- 當(dāng)內(nèi)存過低,即將釋放時(shí)調(diào)用;
* viewDidUnload-當(dāng)內(nèi)存過低,釋放一些不需要的視圖時(shí)調(diào)用。
8. 應(yīng)用生命周期中的幾個(gè)重要方法
* 啟動(dòng)但還沒進(jìn)入狀態(tài)保存 :
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
* 基本完成程序準(zhǔn)備開始運(yùn)行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
* 當(dāng)應(yīng)用程序?qū)⒁敕腔顒?dòng)狀態(tài)執(zhí)行,應(yīng)用程序不接收消息或事件,比如來電話了:
- (void)applicationWillResignActive:(UIApplication *)application
* 當(dāng)應(yīng)用程序入活動(dòng)狀態(tài)執(zhí)行,這個(gè)剛好跟上面那個(gè)方法相反:
- (void)applicationDidBecomeActive:(UIApplication *)application
* 當(dāng)程序被推送到后臺(tái)的時(shí)候調(diào)用。所以要設(shè)置后臺(tái)繼續(xù)運(yùn)行,則在這個(gè)函數(shù)里面設(shè)置即可:
- (void)applicationDidEnterBackground:(UIApplication *)application
* 當(dāng)程序從后臺(tái)將要重新回到前臺(tái)時(shí)候調(diào)用,這個(gè)剛好跟上面的那個(gè)方法相反:
- (void)applicationWillEnterForeground:(UIApplication *)application
* 當(dāng)程序?qū)⒁顺鍪潜徽{(diào)用,通常是用來保存數(shù)據(jù)和一些退出前的清理工作:
- (void)applicationWillTerminate:(UIApplication *)application
9. 判斷對(duì)象是否遵循了某協(xié)議以及代理是否實(shí)現(xiàn)了改代理方法
BOOL isProtocol = [self.delegateController conformsToProtocol:@protocol(TestPtotocol)]);
BOOL isSEL = self.delegate && [self.delegate respondsToSelector:@selector(delegateSel:)]
10. 系統(tǒng)UINavigationController滑動(dòng)返回手勢(shì)取消
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
11. 試圖坐標(biāo)轉(zhuǎn)換
// 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 將像素point從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 將rect從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
*例把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到
controllerA中
// controllerA 中有一個(gè)UITableView, UITableView里有多行UITableVieCell,cell上放有一個(gè)button
// 在controllerA中實(shí)現(xiàn):
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc為btn在controllerA中的rect
或當(dāng)已知btn時(shí):
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
12. 方法的交換
* 實(shí)例方法
+ (void)swizzleSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector {
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethodInit=class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethodInit) {
class_addMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
*類方法
+ (void)swizzleClassSelector:(SEL)originalSelector withClassSelector:(SEL)swizzledSelector {
Class class = [self class];
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
if ((int)originalMethod != 0 && (int)swizzledMethod != 0) {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
13. 利用宏在擴(kuò)展類添加屬性
#define ASSOCIATED(propertyName, setter, type, objc_AssociationPolicy)\
- (type)propertyName {\
return objc_getAssociatedObject(self, _cmd);\
}\
\
- (void)setter:(type)object\
{\
objc_setAssociatedObject(self, @selector(propertyName), object, objc_AssociationPolicy);\
}
14. 漢字轉(zhuǎn)拼音
- (NSString *)stringToPinyin
{
if ([self length] > 0) {
NSMutableString *ms = [[NSMutableString alloc] initWithString:self];
if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
}
if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
//NSLog(@"pinyin: %@", ms);
return ms;
}
}
return self;
}
15. 給空間制定位置添加圓角
- (void)viewAddBezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
16.已某個(gè)view截屏并生成Image
- (UIImage*)viewShot{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
17.Quartz2D相關(guān)
圖形上下是一個(gè)CGContextRef類型的數(shù)據(jù)。
圖形上下文包含:
1,繪圖路徑(各種各樣圖形)
2,繪圖狀態(tài)(顏色,線寬,樣式,旋轉(zhuǎn),縮放,平移)
3,輸出目標(biāo)(繪制到什么地方去?UIView、圖片)
1,獲取當(dāng)前圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
2,添加線條
CGContextMoveToPoint(ctx, 20, 20);
3,渲染
CGContextStrokePath(ctx);
CGContextFillPath(ctx);
4,關(guān)閉路徑
CGContextClosePath(ctx);
5,畫矩形
CGContextAddRect(ctx, CGRectMake(20, 20, 100, 120));
6,設(shè)置線條顏色
[[UIColor redColor] setStroke];
7, 設(shè)置線條寬度
CGContextSetLineWidth(ctx, 20);
8,設(shè)置頭尾樣式
CGContextSetLineCap(ctx, kCGLineCapSquare);
9,設(shè)置轉(zhuǎn)折點(diǎn)樣式
CGContextSetLineJoin(ctx, kCGLineJoinBevel);
10,畫圓
CGContextAddEllipseInRect(ctx, CGRectMake(30, 50, 100, 100));
11,指定圓心
CGContextAddArc(ctx, 100, 100, 50, 0, M_PI * 2, 1);
12,獲取圖片上下文
UIGraphicsGetImageFromCurrentImageContext();
13,保存圖形上下文
CGContextSaveGState(ctx)
14,恢復(fù)圖形上下文
CGContextRestoreGState(ctx)
18.避免同時(shí)點(diǎn)擊多個(gè)Button
第一種全局方式:
在AppDelegate中添加 [[UIButton appearance] setExclusiveTouch:YES];
第二種指定方式:
button.exclusiveTouch = YES;
...后續(xù)繼續(xù)更新...