iOS功能整理

持续更新中... (ps:开头一开始是目录索引,但是不支持,改成列表)

一 UI控件类

  • 1.1 textField的背景图片(防止图片复用)
  • 1.2 tableViewCell 侧滑按钮UITableViewCellDeleteConfirmationView样式更改
  • 1.3 图片按比例缩放
  • 1.4 CALayer图层镂空(可用于app内新手指引)
  • 1.5解决UICollectionView ReloadData闪一下(隐式动画)

二 Foundation基础功能类

  • 2.1 应用名 获取
  • 2.2 在APP内跳转设置
  • 2.3 系统版本宏定义

一 UI控件类


1.textField的背景图片(防止图片复用)

- (UIImage *)textFieldBgImageStr:(NSString *)imageStr
{
    // 加载图片
    UIImage *image = [UIImage imageNamed:imageStr];
    
    // 设置端盖的值
    CGFloat top = image.size.height * 0.5;
    CGFloat left = image.size.width * 0.5;
    CGFloat bottom = image.size.height * 0.5;
    CGFloat right = image.size.width * 0.5;
    
    // 设置端盖的值
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
    // 设置拉伸的模式
    UIImageResizingMode mode = UIImageResizingModeStretch;
    
    // 拉伸图片
    UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
    
    return newImage;
}

2.tableViewCell 侧滑按钮UITableViewCellDeleteConfirmationView样式更改

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *subView in self.subviews) {
            
            if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                UIView *view = ((UIView *)[subView.subviews firstObject]);
                
                //view : <_UITableViewCellActionButton: 0x7fbe61f1b0d0; frame = (132 0; 66 44); opaque = NO; autoresize = H; layer = >
                //view.superview: >
                view.backgroundColor = [UIColor clearColor];
                view.superview.backgroundColor = [UIColor clearColor];
                
                // view.backgroundColor = self.backgroundColor;
                
                NSLog(@"%@", view.subviews[0]);
                /**
                 *view.subviews = {
                 >
                 }
                 */
                //替换字体
                [view.subviews[0] setValue:@"删除" forKey:@"text"];
                //替换字体颜色
                [view.subviews[0] setValue:[UIColor redColor] forKeyPath:@"textColor"];
                
                //也可以直接设置view.layer 但是不会出现边框跟着移动的效果(下图), 这也说明了, UITableViewCellDeleteConfirmationView的frame是跟着你的手指移动在变化的
                view.superview.layer.cornerRadius = 10.0;
                view.superview.layer.borderWidth = 2.0;
                view.superview.layer.borderColor = [UIColor greenColor].CGColor;
                view.superview.layer.masksToBounds = YES;             
            }
        }
    });    
}

//button 已经展示出来button, 将要关闭的时候调用
- (void)didTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *subView in self.subviews) {
            
            if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
                subView.backgroundColor=[UIColor clearColor];
                for (UIView *view in subView.subviews) {
                    if ([view isKindOfClass:[UIButton class]]) {
                        UIButton *btn = (UIButton *)view;
                        btn.backgroundColor=[UIColor clearColor];
                        [btn setImage:[UIImage imageNamed:@"delete_like_user"] forState:UIControlStateNormal];
                        [btn setTitle:nil forState:UIControlStateNormal];
                    }
                }
            }
        }
    });
}

3.图片按比例缩放

/** * @brief Scale Image / 缩放图片 */
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {
    
    if (image.size.width < size.width) {
        return image;
    }
    
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

4.CALayer图层镂空(可用于app内新手指引)

- (void)setupMask {
    // ----- 自己绘制路径path
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;
    [self addSubview:self.toolView];

    CGPathRef path = CGPathCreateWithEllipseInRect(_toolView.frame, 0);
    
    CGMutablePathRef mutablePath = CGPathCreateMutable();
    CGPathAddRect(mutablePath, 0, self.frame);
    CGPathAddPath(mutablePath, 0, path);
    
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = mutablePath;
    mask.fillRule = kCAFillRuleEvenOdd;
    _toolView.layer.mask = mask;
    
    CGPathRelease(path);
    
}

- (void)testForMask {
    // ----- 利用镂空控件的path
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    [self addSubview:self.toolView];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:_toolView.frame cornerRadius:_toolView.frame.size.height/2];
    [path appendPath:circlePath];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    
    _toolView.layer.mask = fillLayer;
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;

}
- (void)demoTest {
    
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;
    [self addSubview:self.toolView];

    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:_toolView.frame cornerRadius:_toolView.frame.size.height/2];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor colorWithWhite:0.3 alpha:0.5].CGColor;
    
    [self.layer addSublayer:fillLayer];
}

5.解决UICollectionView ReloadData闪一下(隐式动画)

这方法有坑:
在增删cell的时候再调用 reloadData 刷新会 crash

也就是
所以方法中的reloadData换成reloadSections就好了

[UIView setAnimationsEnabled:NO];
 
[collectionView performBatchUpdates:^{
    [collectionView reloadData];
} completion:^(BOOL finished) {
    [UIView setAnimationsEnabled:YES];
}];
[UIView animateWithDuration:0 animations:^{
    [collectionView performBatchUpdates:^{
        [collectionView reloadData];
    } completion:nil];
}];

二 Foundation基础功能类


2.1 应用名 获取

NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
if (!appName) {
    appName = [[NSBundle mainBundle] .infoDictionary valueForKey:@"CFBundleName"];
}

2.2 在APP内跳转设置

if (iOS8Later) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    } else {
        NSURL *privacyUrl = [NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"];
        if ([[UIApplication sharedApplication] canOpenURL:privacyUrl]) {
            [[UIApplication sharedApplication] openURL:privacyUrl];
        } else {
            NSString *message = [NSBundle tz_localizedStringForKey:@"Can not jump to the privacy settings page, please go to the settings page by self, thank you"];
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:[NSBundle tz_localizedStringForKey:@"Sorry"] message:message delegate:nil cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"OK"] otherButtonTitles: nil];
            [alert show];
        }
    }

2.3 系统版本宏定义

#define iOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
#define iOS8Later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
#define iOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define iOS9_1Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.1f)

你可能感兴趣的:(iOS功能整理)