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í)就是doubleCFAbsoluteTimetime =CFAbsoluteTimeGetCurrent();
3.程序直接退出
exit(0);
4.超出父視圖范圍的控件部分響應(yīng)事件
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event{UIView* hitView = [superhitTest:point withEvent:event];if(!hitView) {CGPointtempPoint = [_testBtn convertPoint:point fromView:self];if(CGRectContainsPoint(_testBtn.bounds, tempPoint)) {? ? ? ? ? ? hitView = _testBtn;? ? ? ? }? ? }returnhitView;}
5.讓一個(gè)視圖始終在最前面
view.layer.zPosition = 999;
6.判斷一個(gè)view是不是指定view的子視圖
BOOLisChildView =? [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)用程序不接收消息或事件滴须,比如來(lái)電話了:- (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)用,通常是用來(lái)保存數(shù)據(jù)和一些退出前的清理工作:- (void)applicationWillTerminate:(UIApplication*)application
9. 判斷對(duì)象是否遵循了某協(xié)議以及代理是否實(shí)現(xiàn)了改代理方法
BOOLisProtocol = [self.delegateController conformsToProtocol:@protocol(TestPtotocol)]);BOOLisSEL =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):CGRectrc = [cell convertRect:cell.btn.frame toView:self.view];或CGRectrc = [self.view convertRect:cell.btn.frame fromView:cell];// 此rc為btn在controllerA中的rect或當(dāng)已知btn時(shí):CGRectrc = [btn.superview convertRect:btn.frame toView:self.view];或CGRectrc = [self.view convertRect:btn.frame fromView:btn.superview];
12. 方法的交換
* 實(shí)例方法+ (void)swizzleSelector:(SEL)originalSelector withSelector:(SEL)swizzledSelector {? ? Classclass= [selfclass];? ? ? ? Method originalMethod = class_getInstanceMethod(class, originalSelector);? ? Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);BOOLdidAddMethodInit=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 {? ? Classclass= [selfclass];? ? ? ? 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 {\returnobjc_getAssociatedObject(self, _cmd);\}\\- (void)setter:(type)object\{\objc_setAssociatedObject(self,@selector(propertyName), object, objc_AssociationPolicy);\}
14. 漢字轉(zhuǎn)拼音
- (NSString*)stringToPinyin{if([selflength] >0) {NSMutableString*ms = [[NSMutableStringalloc] initWithString:self];if(CFStringTransform((__bridgeCFMutableStringRef)ms,0, kCFStringTransformMandarinLatin,NO)) {? ? ? ? }if(CFStringTransform((__bridgeCFMutableStringRef)ms,0, kCFStringTransformStripDiacritics,NO)) {//NSLog(@"pinyin: %@", ms);returnms;? ? ? ? }? ? }returnself;}
15. 給空間制定位置添加圓角
- (void)viewAddBezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii{UIBezierPath*maskPath = [UIBezierPathbezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];CAShapeLayer*maskLayer = [[CAShapeLayeralloc] 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();returnimage;}
17.Quartz2D相關(guān)
圖形上下是一個(gè)CGContextRef類型的數(shù)據(jù)。圖形上下文包含:1将宪,繪圖路徑(各種各樣圖形)2绘闷,繪圖狀態(tài)(顏色,線寬较坛,樣式印蔗,旋轉(zhuǎn),縮放燎潮,平移)3喻鳄,輸出目標(biāo)(繪制到什么地方去?UIView确封、圖片)1除呵,獲取當(dāng)前圖形上下文CGContextRefctx =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è)置線條顏色[[UIColorredColor] 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中添加 [[UIButtonappearance] setExclusiveTouch:YES];第二種指定方式:button.exclusiveTouch =YES;