美文网首页OC-开发案例收集
iOS中这些牛逼的实用技巧你造吗

iOS中这些牛逼的实用技巧你造吗

作者: 对方正在输入_0 | 来源:发表于2019-06-14 09:08 被阅读116次

    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 创建对象,分配空间

    * init (initWithNibName) 初始化对象,初始化数据

    * loadView 从nib载入视图 ,除非你没有使用xib文件创建视图

    * viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件

    * viewWillAppear视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了

    * viewDidAppear 视图已在屏幕上渲染完成

    * viewWillDisappear 视图将被从屏幕上移除之前执行

    * viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了

    * dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放.

    * viewVillUnload- 当内存过低,即将释放时调用;

    * viewDidUnload-当内存过低,释放一些不需要的视图时调用。

    8. 应用生命周期中的几个重要方法

    * 启动但还没进入状态保存 :- (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions * 基本完成程序准备开始运行:- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions* 当应用程序将要入非活动状态执行,应用程序不接收消息或事件,比如来电话了:- (void)applicationWillResignActive:(UIApplication*)application * 当应用程序入活动状态执行,这个刚好跟上面那个方法相反:- (void)applicationDidBecomeActive:(UIApplication*)application  * 当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可:- (void)applicationDidEnterBackground:(UIApplication*)application  * 当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反:- (void)applicationWillEnterForeground:(UIApplication*)application  * 当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作:- (void)applicationWillTerminate:(UIApplication*)application

    9. 判断对象是否遵循了某协议以及代理是否实现了改代理方法

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

    10. 系统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中实现: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类型的数据。图形上下文包含:1,绘图路径(各种各样图形)2,绘图状态(颜色,线宽,样式,旋转,缩放,平移)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;

    相关文章

      网友评论

        本文标题:iOS中这些牛逼的实用技巧你造吗

        本文链接:https://www.haomeiwen.com/subject/yinyfctx.html