iOS实践(持续更新...)

我只是一个搬运工......在别处看到的关于iOS开发的小tips以及自己在项目开发过程中遇到的小问题,一起整理整理。

1.利用CGGeometry函数获取相关数据:

CGRect frame = self.view.frame;
CGFloat x = CGRectGetMinX(frame);
CGFloat y = CGRectGetMinY(frame);
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);

2.文本高度的计算:
对于单行文本数据的显示调用- (CGSize)sizeWithAttributes:(NSDictionary *)attrs;方法来得到文本宽度和高度; 对于多行文本数据的显示调用- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context ;方法来得到文本宽度和高度;同时注意在此之前需要设置文本控件的numberOfLines属性为0。通常我们会在自定义Cell中设置一个高度属性,用于外界方法调用,因为Cell内部设置Cell的高度是没有用的,UITableViewCell在初始化时会重新设置高度。

3.调试:实时改变背景颜色

(lldb) e aview.backgroundColor = [UIColor redColor]
(lldb) e (void)[CATransaction flush]

4.正确获取collection view的contentSize的方法

 [collectionView.collectionViewLayout collectionViewContentSize]

5.UNRECOGNIZED SELECTOR SENT TO INSTANCE 问题快速定位的方法
添加符号断点:-[NSObject(NSObject) doesNotRecognizeSelector:]

6.prepareForUse方法

if the cell is reusable (has a reuse identifier),
this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:.
If you override, you MUST call super.

如果cell中有图片存在的话,可以在cell中重写此方法,在cell可用前做一些清理工作。

7.界面中存在多个button,防止多个button同时响应事件

button.exclusiveTouch = YES

8.AVMakeRectWithAspectRatioInsideRect图片居中

9.过滤特殊字符

// 定义一个特殊字符的集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:;()¥「」"、[]{}#%-*+=_\|~<>$€^•'@#$%^&*()_+'""];
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];

10.去掉分割线

首先在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];    
    }
}

11.计算方法耗时时间

// 获取时间间隔
#define TICK   CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

12.GCD group

dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {    
  // block1    NSLog(@"Block1");    
  [NSThread sleepForTimeInterval:5.0];    
  NSLog(@"Block1 End");
});
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {    
  // block2    NSLog(@"Block2");    
  [NSThread sleepForTimeInterval:8.0];   
  NSLog(@"Block2 End");
});
dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {    
  // block3    NSLog(@"Block3");
});
// only for non-ARC projects, handled automatically in ARC-enabled projects.
dispatch_release(group);

13.凸版印刷体效果
就是给文字加上奇妙阴影和高光,让文字看起来有凹凸感,像是被压在屏幕上。设置很简单:

NSTextEffectAttributeName:NSTextEffectLetterpressStyle。

14.去掉backBarButtonItem文字

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                      forBarMetrics:UIBarMetricsDefault];

15.假设这样一种情况,cell中存在进度条,而进度条需要动画,在tableView滚动的过程当中不产生动画,滚动停止之后开始动画。拿到需求之后,第一个想到的肯定是重写

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

这个方法,在里面做动画,天真如我很快完成。但是bug已经产生了,就是在缓慢滑动之后动画是不会开始的,因为它没有减速的过程所有也不会调用上面的方法。最后,利用runloop解决,一句话的事情:

[self performSelector:@selector(animatePercentLine) withObject:nil afterDelay:0.0f inModes:@[NSDefaultRunLoopMode]];

16.图片压缩,将图片尺寸限制在600以内(图片大了真伤神,特别容易引起内存警告,压缩在600以内或许是个不错的选择,图片还是很清晰的,关键是上传速度快)

      if (MIN(image.size.width, image.size.height) > 600) {
            CGFloat oldWidth = CGImageGetWidth(image.CGImage);
            CGFloat oldHeight = CGImageGetHeight(image.CGImage);
            CGFloat newWidth = 0;
            CGFloat newHeight = 0;
            if (oldHeight > oldWidth) {
                CGFloat rate = oldHeight / 600.0;
                newWidth = oldWidth / rate;
                newHeight = 600;
            }
            else {
                CGFloat rate = oldWidth / 600.0;
                newHeight = oldHeight / rate;
                newWidth = 600;
            }
            uploadImage = [self imageByScalingAndCroppingForSize:CGSizeMake(newWidth, newHeight) image:image];
        }
        self.imageView.image = uploadImage;
        AVMakeRectWithAspectRatioInsideRect(uploadImage.size, self.imageView.bounds);
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage *)image {
    
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    UIGraphicsBeginImageContext(targetSize);
    [sourceImage drawInRect:(CGRect){0,0, targetSize}];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

你可能感兴趣的:(iOS实践(持续更新...))