iOS功能整理

2016-12-28  本文已影响0人  z_zero
持续更新中... (ps:开头一开始是目录索引,但是简书不支持,改成列表)

一 UI控件类

二 Foundation基础功能类


<h2 id="一">一 UI控件类</h2>
<h4 id="1.1">1.textField的背景图片(防止图片复用)</h4>

- (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;
}

<h4 id="1.2">2.tableViewCell 侧滑按钮UITableViewCellDeleteConfirmationView样式更改</h4>

- (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 = <CALayer: 0x7fbe61f1cd50>>
                //view.superview: <UITableViewCellDeleteConfirmationView: 0x7fbe61de34d0; frame = (415.333 0; 0 44); clipsToBounds = YES; autoresize = H; layer = <CALayer: 0x7fbe61de3680>>
                view.backgroundColor = [UIColor clearColor];
                view.superview.backgroundColor = [UIColor clearColor];
                
                // view.backgroundColor = self.backgroundColor;
                
                NSLog(@"%@", view.subviews[0]);
                /**
                 *view.subviews = {
                 <UIButtonLabel: 0x7fa769549440; frame = (15 11; 52.3333 21.6667); text = 'Delete'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa769548cd0>>
                 }
                 */
                //替换字体
                [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];
                    }
                }
            }
        }
    });
}

<h4 id="1.3">3.图片按比例缩放</h4>

/** * @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;
}

<h4 id="1.4">4.CALayer图层镂空(可用于app内新手指引)</h4>

- (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];
}

<h4 id="1.5">5.解决UICollectionView ReloadData闪一下(隐式动画)</h4>

这方法有坑:
在增删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];
}];

<h2 id="二">二 Foundation基础功能类</h2>
<h4 id="2.1">2.1 应用名 获取</h4>

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

<h4 id="2.2">2.2 在APP内跳转设置</h4>

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];
        }
    }

<h4 id="2.3">2.3 系统版本宏定义</h4>

#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)

上一篇下一篇

猜你喜欢

热点阅读