iOS开发一些小技巧

叶神的博客1
叶神的博客2
叶神的博客3

获取APP的Launch Image

如果有个需求,我们想在APP内部还想继续使用LaunchImage的话,通常的方法是将launchImage 加入到工程中,然后使用的时候判断设备获取不同尺寸的启动图。但是这个方法比较原始,而且需要多占用一份资源。

里脊串串哥的博客上看到一种方法。可以直接获取到LaunchImage,关键代码如下:

CGSize viewSize = self.window.bounds.size;
NSString *viewOrientation = @"Portrait";    //横屏请设置成 @"Landscape"
NSString *launchImage = nil;

NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary* dict in imagesDict) {
    CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
    if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
        launchImage = dict[@"UILaunchImageName"];
    }
}

PS:串哥博客
串哥微博

一些容易忽视的技巧


Rect
生成一个距离原Rect的左右边距为dx,上下边距为dy的Rect
CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)

判断两个Rect是否重叠
CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)

生成一个CGRectMake(0,0,0,0)
CGRectZero
隐藏状态栏
[UIApplication sharedApplication] setStatusBarHidden:<#(BOOL)#> withAnimation:<#(UIStatusBarAnimation)#>
TableView
划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

让TableViewCell的textLabel.text 缩进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;

改变换行线颜色
tableView.separatorColor = [UIColor blueColor];

跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

设置滚动条的颜色
self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

获取到触摸点的位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSUInteger numTaps = [touch tapCount];
    CGPoint point = [touch locationInView:self.view];
}
- (CGPoint)locationInView:(UIView *)view;  //获取到的Point为在整个屏幕上的位置
- (CGPoint)previousLocationInView:(UIView *)view; //获取到的位置在当前view上的位置

UITouch的属性
@property(nonatomic,readonly) NSTimeInterval      timestamp;
@property(nonatomic,readonly) UITouchPhase        phase;
@property(nonatomic,readonly) NSUInteger          tapCount;

获取项目中文件
获取Documents目录
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

获取tmp目录
NSString *tmpPath = NSTemporaryDirectory();

从Plist文件中加载字典或数组
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *array = [NSArray arrayWithContentsOfFile:path];

网络指示器
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:visible];

IB 运行时属性

在IB中直接为控件设置圆角。


image

当然还有很多其他用法

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、隐藏导航栏的正确姿势 套路一:在viewWillAppear 和 viewWillDisappear方法里面用...
    Arxu阅读 198评论 0 2
  • 最近,彭于晏余文乐主演的电影《悟空传》上映了,面对两极化的评论,让我感到十分的好奇,于是去影院看了这部片子。也许是...
    白驹过隙2016阅读 408评论 0 1
  • 劝慰自己看书吧,好书能教化人如何静,静到一面镜。 假期无事,去逛了本书,张箐居士写的《红尘外的茶香》。茶...
    EchoGanli阅读 574评论 0 0
  • 今天的晨读是《幸福的方法》,一个主动追求幸福的人,相信幸福也会转过身来敲他的门。 这两天也因为工作的事情遇到了...
    令芳老师阅读 360评论 2 7