1、判斷一個點或一個區(qū)域是否在某個指定的區(qū)域
/* Return true if `point' is contained in `rect', false otherwise. */
CG_EXTERN bool CGRectContainsPoint(CGRect rect, CGPoint point)
CG_AVAILABLE_STARTING(10.0, 2.0);
/* Return true if `rect2' is contained in `rect1', false otherwise. `rect2'
is contained in `rect1' if the union of `rect1' and `rect2' is equal to
`rect1'. */
CG_EXTERN bool CGRectContainsRect(CGRect rect1, CGRect rect2)
CG_AVAILABLE_STARTING(10.0, 2.0);
2偶器、調(diào)整按鈕內(nèi)部imageView的大小
- (CGRect)imageRectForContentRect:(CGRect)contentRect
//去調(diào)整按鈕內(nèi)部imageView的大小
//返回按鈕內(nèi)部imageView尺寸位置
//contentRect:當前按鈕的尺寸大小.
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
CGFloat w = 40;
CGFloat h = 45;
CGFloat x = (contentRect.size.width-w)*0.5;
CGFloat y = 20;
CGRect rect = CGRectMake(x, y, w, h);
return rect;
}
3、設(shè)置一個按鈕內(nèi)部文字顯示不同的文字屬性
調(diào)用按鈕的setAttributedTitle方法畅形,根據(jù)range去設(shè)置不同位置文字的不同屬性。
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state
//普通狀態(tài)
NSString *first = @"VIP";
NSString *second = @"100元";
NSString *btnString = [NSString stringWithFormat:@"%@\n%@", first, second];
NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:btnString];
NSDictionary *dict = @{NSForegroundColorAttributeName : [UIColor yellowColor],NSFontAttributeName : [UIFont systemFontOfSize:15]};
NSRange range = NSMakeRange(first.length+1, second.length);
[attriStr addAttributes:dict range:range];
button.titleLabel.numberOfLines = 0;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setAttributedTitle:attriStr forState:UIControlStateNormal];
4、根據(jù)一張大圖去裁剪成很多張小圖片
CGImageRef __nullable CGImageCreateWithImageInRect( CGImageRef cg_nullable image, CGRect rect)
// 計算裁剪小圖片的x坐標
littleImageX = i * littleImageW;
// 裁剪小圖片并設(shè)置
CGImageRef littleImage = CGImageCreateWithImageInRect(origImage.CGImage, CGRectMake(littleImageX, 0, littleImageW, littleImageH));
UIImage *littleImageN = [UIImage imageWithCGImage:littleImage];
[btn setImage:littleImageN forState:UIControlStateNormal];
5搔弄、從上下文中清除某個區(qū)域
// 在上下文中清除Rect內(nèi)的內(nèi)容
CGContextClearRect(ctx, clearRect);
6、清除形變
self.view.transform = CGAffineTransformIdentity;
7丰滑、UIView動畫的彈簧效果
Duration:動畫的執(zhí)行時長
delay:動畫延時長.
Damping:動畫的彈性系數(shù),越小,彈簧效果越明顯
initialSpringVelocity:彈簧初始化速度
[UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear animations:^{
//動畫執(zhí)?行代碼
} completion:^(BOOL finished) {
//動畫完成時調(diào)?用.
}];
8顾犹、獲取全局的(整個應(yīng)用程序)導(dǎo)航條
UINavigationBar *bar = [UINavigationBar appearance];
獲取指定類下的導(dǎo)航條,此方法常用在導(dǎo)航條類的+ (void)initialize方法中,對整個app的導(dǎo)航條做一次性的內(nèi)容設(shè)置褒墨。
//獲取指定類下的導(dǎo)航條
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[
[self class]
]
];
9炫刷、導(dǎo)航條達到透明的效果
/*
設(shè)置導(dǎo)航條透明度為0,沒有效果,還是原來的樣?. 原因是因為導(dǎo)航條上面那一塊并不直接是導(dǎo)航條,它是導(dǎo)航條里面的?個子控件. 所以在這里設(shè)置它沒有效果,因為系統(tǒng)會生成?個半透明的圖片.
self.navigationController.navigationBar.alpha = 0.0;
所以在這里我們可以考慮給它設(shè)置一個半透明的圖片. 在這里,有一個模式,必須要傳默認UIBarMetricsDefault模式. 在這里發(fā)現(xiàn)設(shè)為nil的時候,也沒有效果,那是因為系統(tǒng)它做了?層判斷,它會判斷如果傳入的系統(tǒng)圖片為空的話,它就會幫你生成一個半透明的圖片,設(shè)置導(dǎo)航條的背景圖片.
**/
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
10、獲取不需要渲染的原始圖片
UIImage *image = [UIImage imageNamed:imageName];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
11郁妈、當tap手勢和點擊事件都存在是浑玛,默認手勢的優(yōu)先級更高,Touch不起作用噩咪,解決方法
實現(xiàn)UIGestureRecognizerDelegate
方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
/**
called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
在touchesBegan:withEvent:之前調(diào)用顾彰,手勢識別器獲取一個新touch,返回NO,不讓手勢識別器識別該touch
@return NO:不讓手勢識別器識別該touch; YES:讓手勢識別器識別該touch
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
} else {
return YES;
}
}
12剧腻、更新tableview時自動加一個序列幀動畫
//更新數(shù)據(jù)(當更新tableView時會自動加一個序列幀動畫)
[tableView beginUpdates];
[tableView endUpdates];
13拘央、nil和NSNull區(qū)別
[NSNull null]; //空對象
nil //空
NSNull *null = [NSNull null];
NSDictionary *dict = @{@"name":@"zhangsan",@"age":[NSNull null]};
持續(xù)更行中........