iOS杂碎2

2018-11-12  本文已影响35人  爨乡的云

NSPredicate用法之一: 数组元素为对象时,通过对象的某个属性快速筛选出数组中的对象

// 找出tips和年报的模型
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == 1001 OR type == 1002"];
NSArray *temp = [self.dataArray filteredArrayUsingPredicate:predicate];
if (temp.count > 0) {
   _hasTips = YES;
} else {
    _hasTips = NO;
}

关于NSPredicate更多高级用法,参见:iOS NSPredicate 使用详解
iOS中的谓词(NSPredicate)使用

SD图片加载渐现效果

[_ImageView sd_setImageWithURL:url placeholderImage:placeholderImage options:YYWebImageOptionAllowBackgroundTask progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
    dispatch_sync(dispatch_get_main_queue(), ^(){
       [weakSelf fadeLayer:weakSelf.ImageView.layer];
    });
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];

- (void)fadeLayer:(CALayer *)layer {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [layer addAnimation:transition forKey:@"fade"];
}

使用pandoc为markdown生成大纲

pandoc 2.5
Compiled with pandoc-types 1.17.5.4, texmath 0.11.1.2, skylighting 0.7.4
Default user data directory: /Users/wanghaobing/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org

WKWebView头部添加自定义View, 随Webview一起滚动

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) configuration:configuration];
webView.navigationDelegate = self;
webView.multipleTouchEnabled = YES;
webView.userInteractionEnabled = YES;
webView.contentMode = UIViewContentModeScaleAspectFit;
webView.scrollView.scrollEnabled = YES;
webView.scrollView.delegate = self;
        
// 调整contentInset,将自定义视图添加到webView.scrollView
webView.scrollView.contentInset = UIEdgeInsetsMake(ScreenWidth, 0, 0, 0);

CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(0, -ScreenWidth, ScreenWidth, ScreenWidth)]; //注意y值.
[webView.scrollView addSubview: customView];

[self.view addSubview:webView];

获取当天,当周,当月,当年的时间区间

double interval = 0;
NSDate *beginDate = nil;
NSDate *endDate = nil;
    
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];//设定周一为周首日
BOOL exist = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&beginDate interval:&interval forDate:[NSDate date]];
// unit可分别修改为 NSCalendarUnitDay NSCalendarUnitWeekOfYear NSCalendarUnitMonth NSCalendarUnitYear
if (exist) {
   endDate = [beginDate dateByAddingTimeInterval:interval-1];
}else {
   return [NSArray array];
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *beginString = [myDateFormatter stringFromDate:beginDate];
NSString *endString = [myDateFormatter stringFromDate:endDate];
    
NSString *section = [NSString stringWithFormat:@"%@ ~ %@",beginString,endString];
NSLog(@"%@",section); // 2018-11-12 00:00:00 ~ 2018-11-18 23:59:59

当然,将上述方法中的 rangeOfUnit 替换为:

iOS 11以后系统相册选取完图片编辑页面,取消按钮很难被点击到的问题解决办法

#pragma mark - Navigation Controller Delegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([UIDevice currentDevice].systemVersion.floatValue < 11) {
        return;
    } if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) {
        [viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // iOS 11之后,图片编辑界面最上层会出现一个宽度<42的view,会遮盖住左下方的cancel按钮,使cancel按钮很难被点击到,故改变该view的层级结构
            if (obj.frame.size.width < 42) {
                [viewController.view sendSubviewToBack:obj];
                *stop = YES;
            }
        }];
    }
}

创建一个可任意伸缩的图片

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

Article list

上一篇 下一篇

猜你喜欢

热点阅读