iOS中這些牛逼的實用技巧你造嗎

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. 簡單的獲取當前時間

// CFAbsoluteTime其實就是doubleCFAbsoluteTimetime =CFAbsoluteTimeGetCurrent();

3.程序直接退出

exit(0);

4.超出父視圖范圍的控件部分響應事件

-(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.讓一個視圖始終在最前面

view.layer.zPosition = 999;

6.判斷一個view是不是指定view的子視圖

BOOLisChildView =? [childView isDescendantOfView:parentView];

7. UIViewController中的幾個重要方法

* alloc 創(chuàng)建對象,分配空間

* init (initWithNibName) 初始化對象,初始化數(shù)據(jù)

* loadView 從nib載入視圖 ,除非你沒有使用xib文件創(chuàng)建視圖

* viewDidLoad 載入完成,可以進行自定義數(shù)據(jù)以及動態(tài)創(chuàng)建其他控件

* viewWillAppear視圖將出現(xiàn)在屏幕之前,馬上這個視圖就會被展現(xiàn)在屏幕上了

* viewDidAppear 視圖已在屏幕上渲染完成

* viewWillDisappear 視圖將被從屏幕上移除之前執(zhí)行

* viewDidDisappear 視圖已經被從屏幕上移除,用戶看不到這個視圖了

* dealloc 視圖被銷毀,此處需要對你在init和viewDidLoad中創(chuàng)建的對象進行釋放.

* viewVillUnload- 當內存過低,即將釋放時調用;

* viewDidUnload-當內存過低,釋放一些不需要的視圖時調用。

8. 應用生命周期中的幾個重要方法

* 啟動但還沒進入狀態(tài)保存 :- (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions * 基本完成程序準備開始運行:- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions* 當應用程序將要入非活動狀態(tài)執(zhí)行,應用程序不接收消息或事件,比如來電話了:- (void)applicationWillResignActive:(UIApplication*)application * 當應用程序入活動狀態(tài)執(zhí)行,這個剛好跟上面那個方法相反:- (void)applicationDidBecomeActive:(UIApplication*)application? * 當程序被推送到后臺的時候調用。所以要設置后臺繼續(xù)運行,則在這個函數(shù)里面設置即可:- (void)applicationDidEnterBackground:(UIApplication*)application? * 當程序從后臺將要重新回到前臺時候調用,這個剛好跟上面的那個方法相反:- (void)applicationWillEnterForeground:(UIApplication*)application? * 當程序將要退出是被調用,通常是用來保存數(shù)據(jù)和一些退出前的清理工作:- (void)applicationWillTerminate:(UIApplication*)application

9. 判斷對象是否遵循了某協(xié)議以及代理是否實現(xiàn)了改代理方法

BOOLisProtocol = [self.delegateController conformsToProtocol:@protocol(TestPtotocol)]);BOOLisSEL =self.delegate && [self.delegate respondsToSelector:@selector(delegateSel:)]

10. 系統(tǒng)UINavigationController滑動返回手勢取消

self.navigationController.interactivePopGestureRecognizer.enabled =NO;

11. 試圖坐標轉換

// 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值- (CGPoint)convertPoint:(CGPoint)point toView:(UIView*)view;// 將像素point從view中轉換到當前視圖中,返回在當前視圖中的像素值- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView*)view;// 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect- (CGRect)convertRect:(CGRect)rect toView:(UIView*)view;// 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect- (CGRect)convertRect:(CGRect)rect fromView:(UIView*)view;*例把UITableViewCell中的subview(btn)的frame轉換到controllerA中// controllerA 中有一個UITableView, UITableView里有多行UITableVieCell,cell上放有一個button// 在controllerA中實現(xiàn):CGRectrc = [cell convertRect:cell.btn.frame toView:self.view];或CGRectrc = [self.view convertRect:cell.btn.frame fromView:cell];// 此rc為btn在controllerA中的rect或當已知btn時:CGRectrc = [btn.superview convertRect:btn.frame toView:self.view];或CGRectrc = [self.view convertRect:btn.frame fromView:btn.superview];

12. 方法的交換

* 實例方法+ (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. 利用宏在擴展類添加屬性

#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. 漢字轉拼音

- (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.已某個view截屏并生成Image

- (UIImage*)viewShot{UIGraphicsBeginImageContext(self.bounds.size);? ? [self.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage*image =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnimage;}

17.Quartz2D相關

圖形上下是一個CGContextRef類型的數(shù)據(jù)。圖形上下文包含:1,繪圖路徑(各種各樣圖形)2,繪圖狀態(tài)(顏色,線寬,樣式,旋轉,縮放,平移)3,輸出目標(繪制到什么地方去?UIView、圖片)1,獲取當前圖形上下文CGContextRefctx =UIGraphicsGetCurrentContext();2,添加線條CGContextMoveToPoint(ctx,20,20);3,渲染CGContextStrokePath(ctx);CGContextFillPath(ctx);4,關閉路徑CGContextClosePath(ctx);5,畫矩形CGContextAddRect(ctx,CGRectMake(20,20,100,120));6,設置線條顏色[[UIColorredColor] setStroke];7, 設置線條寬度CGContextSetLineWidth(ctx,20);8,設置頭尾樣式CGContextSetLineCap(ctx, kCGLineCapSquare);9,設置轉折點樣式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,恢復圖形上下文CGContextRestoreGState(ctx)

18.避免同時點擊多個Button

第一種全局方式:在AppDelegate中添加 [[UIButtonappearance] setExclusiveTouch:YES];第二種指定方式:button.exclusiveTouch =YES;

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容