持續(xù)更新中... (ps:開頭一開始是目錄索引斩熊,但是簡書不支持何荚,改成列表)
一 UI控件類
- 1.1 textField的背景圖片(防止圖片復用)
- 1.2 tableViewCell 側(cè)滑按鈕UITableViewCellDeleteConfirmationView樣式更改
- 1.3 圖片按比例縮放
- 1.4 CALayer圖層鏤空(可用于app內(nèi)新手指引)
- 1.5解決UICollectionView ReloadData閃一下(隱式動畫)
二 Foundation基礎(chǔ)功能類
- 2.1 應(yīng)用名 獲取
- 2.2 在APP內(nèi)跳轉(zhuǎn)設(shè)置
- 2.3 系統(tǒng)版本宏定義
<h2 id="一">一 UI控件類</h2>
<h4 id="1.1">1.textField的背景圖片(防止圖片復用)</h4>
- (UIImage *)textFieldBgImageStr:(NSString *)imageStr
{
// 加載圖片
UIImage *image = [UIImage imageNamed:imageStr];
// 設(shè)置端蓋的值
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;
// 設(shè)置端蓋的值
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
// 設(shè)置拉伸的模式
UIImageResizingMode mode = UIImageResizingModeStretch;
// 拉伸圖片
UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
return newImage;
}
<h4 id="1.2">2.tableViewCell 側(cè)滑按鈕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"];
//也可以直接設(shè)置view.layer 但是不會出現(xiàn)邊框跟著移動的效果(下圖), 這也說明了, 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 已經(jīng)展示出來button, 將要關(guān)閉的時候調(diào)用
- (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內(nèi)新手指引)</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的時候再調(diào)用 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基礎(chǔ)功能類</h2>
<h4 id="2.1">2.1 應(yīng)用名 獲取</h4>
NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
if (!appName) {
appName = [[NSBundle mainBundle] .infoDictionary valueForKey:@"CFBundleName"];
}
<h4 id="2.2">2.2 在APP內(nèi)跳轉(zhuǎn)設(shè)置</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 系統(tǒng)版本宏定義</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)