點擊return取消textView 的響應者
- - (BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- [_contactTextFiled resignFirstResponder];
- return YES;
- }
-
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
-
- if([text isEqualToString:@"\n"]){
-
- [textView resignFirstResponder];
- [_contactTextFiled becomeFirstResponder];
- return YES;
- }
- return YES;
- }
這裏主要利用了一個makeObjectsPerformSelector:
函數。這個函數能夠在不少場景下使用從而簡化代碼。
- [xxxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
有效解決刷新單個cell或者section閃一下的問題:
- [UIView setAnimationsEnabled:NO];
- [_listTable beginUpdates];
- [_listTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
- [_listTable endUpdates];
- [UIView setAnimationsEnabled:YES];
每日更新關注:http://weibo.com/hanjunqiang 新浪微博!xcode
畫出下列曲線:
- UIView *myCustomView = [[UIView alloc]initWithFrame:CGRectMake(0, 204,kScreenWidth, 120)];
- myCustomView.backgroundColor = [UIColor whiteColor];
- [view addSubview:myCustomView];
-
- UIBezierPath *bezierPath = [UIBezierPath bezierPath];
- [bezierPath moveToPoint:CGPointMake(0,0)];
- [bezierPath addCurveToPoint:CGPointMake(myCustomView.width, 0) controlPoint1:CGPointMake(0, 0) controlPoint2:CGPointMake(myCustomView.width/2, 40)];
- [bezierPath addLineToPoint:CGPointMake(myCustomView.width, myCustomView.height)];
- [bezierPath addLineToPoint:CGPointMake(0, myCustomView.height)];
- [bezierPath closePath];
-
- CAShapeLayer *shapLayer = [CAShapeLayer layer];
- shapLayer.path = bezierPath.CGPath;
- myCustomView.layer.mask = shapLayer;
- myCustomView.layer.masksToBounds = YES;
當你使用 UISearchController 在 UITableView 中實現搜索條,在搜索框已經激活並推入新的 VC 的時候會發生搜索框重疊的狀況:那就是 definesPresentationContext 這個布爾值!
每日更新關注:http://weibo.com/hanjunqiang 新浪微博!app
TabBar的隱藏與消失:
- - (void)hidesTabBar:(BOOL)hidden{
- [UIView beginAnimations:nil context:NULL];
- [UIView setAnimationDuration:0];
-
- for (UIView *view in self.tabBarController.view.subviews){
-
- if ([view isKindOfClass:[UITabBar class]]) {
- if (hidden) {
- [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height, view.frame.size.width , view.frame.size.height)];
- }else{
- [view setFrame:CGRectMake(view.frame.origin.x, [UIScreen mainScreen].bounds.size.height - 49, view.frame.size.width, view.frame.size.height)];
- }
- }else{
- if([view isKindOfClass:NSClassFromString(@"UITransitionView")]){
- if (hidden){
- [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
- }else{
-
- [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 49 )];
- }
- }
- }
- }
- [UIView commitAnimations];
- }
獲取cell上按鈕所在分區和行數:
- UIView *view = [sender superview];
- GCCollectGroupCellTableViewCell *cell = (GCCollectGroupCellTableViewCell*)[view superview];
- NSIndexPath *indexPath = [_listTable indexPathForCell:cell];
- NSLog(@"%@",detailArr[indexPath.section-1][indexPath.row][@"comname"]);
NSAttributedString 與NSString互轉:
- NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[_CompanyFileds.comname dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
- _CompanyFileds.comname = attrStr.string;
監控UITextField 變化:
- [[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidChangeNotification object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeForKeyWord:) name:UITextFieldTextDidChangeNotification object:nil];
-
-
- - (void)changeForKeyWord:(NSNotification *)sender
- {
- [self checkNum:_pageTextField.text];
- }
- - (BOOL)checkNum:(NSString *)str
- {
- NSString *regex = @"^[0-9]+(.[0-9]{2})?$";
- NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- BOOL isMatch = [pred evaluateWithObject:str];
- if (!isMatch && _pageTextField.text.length>0) {
- UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"頁碼只能輸入數字哦" delegate:self cancelButtonTitle:@"從新輸入" otherButtonTitles:nil, nil nil];
- alertView.delegate = self;
- [alertView show];
- return NO;
- }
- return YES;
- }
監聽UITextField的點擊事件
- [[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidBeginEditingNotification object:nil];
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterEdited:) name:UITextFieldTextDidBeginEditingNotification object:nil];
-
- - (void)enterEdited:(NSNotification *)sender
- {
- 事件寫這裏!但願幫到你!
- }
每日更新關注:http://weibo.com/hanjunqiang 新浪微博!ide
解決添加到父視圖手勢影響子視圖手勢的解決辦法:(手勢衝突)
- #pragma mark--UIGestureRecognizerDelegate
- -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
- {
- if([touch.view isKindOfClass:[UIButton class]]||[touch.view isKindOfClass:[UITableViewCell class]]||[touch.view isKindOfClass:[UITextField class]]||[touch.view isKindOfClass:[UITextView class]])
- {
- return NO;
- }
- return YES;
- }
UITextField 邊框樣式及內容調整:
- _textBoxName.layer.borderWidth=1.0f;
- _textBoxName.layer.borderColor=[UIColorcolorWithRed:0xbf/255.0fgreen:0xbf/255.0fblue:0xbf/255.0falpha:1].CGColor;
-
-
- _textBoxName.leftView=[[UIViewalloc] initWithFrame:CGRectMake(0,0, 16,51)];
- _textBoxName.leftViewMode=UITextFieldViewModeAlways;
圖片旋轉控制:
- #pragma mark ----- 更新按鈕動畫
- - (void)rotate360DegreeWithImageViews:(UIImageView *)myViews{
- CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
- rotationAnimation.duration = 1.0;
- rotationAnimation.cumulative = YES;rotate360DegreeWithImageViews
- rotationAnimation.repeatCount = 100000;
- [myViews.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
- }
- [myViews.layer removeAllAnimations];
改變cell的選中顏色:
- cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
- cell.selectedBackgroundView.backgroundColor = COLOR_BACKGROUNDVIEW;
- 不須要任何顏色能夠這麼設置:
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
取整問題:
- Objective-C拓展了C,天然不少用法是和C一致的。好比浮點數轉化成整數,就有如下四種狀況。
- 1.簡單粗暴,直接轉化
-
-
- float f = 1.5; int a; a = (int)f; NSLog("a = %d",a);
- 輸出結果是1。(int)是強制類型轉化,丟棄浮點數的小數部分。
-
- 2.高斯函數,向下取整
-
-
- float f = 1.6; int a; a = floor(f); NSLog("a = %d",a);
- 輸出結果是1。floor()方法是向下取整,相似於數學中的高斯函數 [].取得不大於浮點數的最大整數,對於正數來講是捨棄浮點數部分,對於複數來講,捨棄浮點數部分後再減1.
-
- 3.ceil函數,向上取整。
-
-
- float f = 1.5; int a; a = ceil(f); NSLog("a = %d",a);
- 輸出結果是2。ceil()方法是向上取整,取得不小於浮點數的最小整數,對於正數來講是捨棄浮點數部分並加1,對於複數來講就是捨棄浮點數部分.
-
- 4.經過強制類型轉換四捨五入。
-
- float f = 1.5; int a; a = (int)(f+0.5); NSLog("a = %d",a);