標籤:dia 阻塞 anim join als uicolor ack test avr
1.導覽列修改title並帶動畫屬性
效果
這個需要用到CATransition。樣本如下
//設施導覽列標題,直接使用self.title是沒有動畫效果的-(void)setNav{ UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)]; lab.text = @"導覽列"; lab.textColor = [UIColor blackColor]; self.navigationItem.titleView = lab;}
//切換標題動畫-(void)clickNavRightBar{ CATransition *animation = [CATransition animation]; animation.duration = 1.0; animation.type = kCATransitionPush; animation.subtype = kCATransitionFromTop; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [self.navigationItem.titleView.layer addAnimation:animation forKey:@"changeTitle"]; NSString *titleStr = ((UILabel*)self.navigationItem.titleView).text; if ([titleStr isEqualToString:@"導覽列"]) { ((UILabel*)self.navigationItem.titleView).text = @"換了"; } else { ((UILabel*)self.navigationItem.titleView).text = @"導覽列"; }}
2.GCD中group函數的使用
假如一個介面有兩部分a和b組成,但資料由兩個介面返回。現在要求是只有等兩個介面的資料都載入完成後才能進行下一步操作。
那麼可以用gcd的group函數
dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //a部分資料 [self doSomethingA];});dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //b部分資料 [self doSomethingB];});dispatch_group_notify(group, dispatch_get_main_queue(), ^{ //請求完畢後處理});
其中,doSomethingA和doSomethingB都是同步處理時,這樣是沒問題的。
若a、b處理資料時用的是block,並且是非同步。那麼這樣寫就會出問題。因為非同步不會阻塞當前線程,所以就會出現a、b資料還沒處理完就開始執行“請求完畢後再進行的操作”。所以這時應為
dispatch_group_t group = dispatch_group_create();dispatch_group_enter(group);[[TestBlockModel sharedInstance]executeWithStr:nil block:^(NSString *parameter) { //處理a部分資料 //………… dispatch_group_leave(group);}];dispatch_group_enter(group);[[TestBlockModel sharedInstance]executeWithStr:nil block:^(NSString *parameter) { //處理b部分資料 //………… dispatch_group_leave(group);}];dispatch_group_notify(group, dispatch_get_main_queue(), ^{ //請求完畢後處理});
3.修改UISearchBar清除按鈕的顏色,可以用
UIImage *imgClear = [UIImage imageNamed:@"nav_icon_close"];[searchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];[searchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
4.圖片點擊方法
當一個view上有多個控制項,這時想寫點擊圖片的方法。那麼可以用:
之前的寫法是通過
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kHeadIconHeight, kHeadIconHeight)];imgView.image = [UIImage imageNamed:@"head_icon"];imgView.userInteractionEnabled = YES;UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHeadIcon)];[imgView addGestureRecognizer:tap];-(void)clickHeadIcon{ NSLog(@"點擊頭像");}
這種寫法最常見。現在可以用另一種方式解決,這種方法大部分與上邊一致,不同的只是不用填加imgView.userInteractionEnabled = YES這個方法。用點擊的點是否在imgView內來判斷是否觸發事件發生。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { // 判斷點擊的點,在不在圓內 CGPoint center = self.imgView.center; CGFloat r = self.imgView.frame.size.width * 0.5; CGFloat newR = sqrt((center.x - point.x) * (center.x - point.x) + (center.y - point.y) * (center.y - point.y)); if (newR > r) { return false; } else { return true; }}
5.label自適應字型
之前都是label字型、高度確定,讓寬度適應。現在有需求是寬度、高度一定,讓字型自適應。可以用
label.adjustsFontSizeToFitWidth = YES;
這一行代碼就足夠了
6.判斷字串是否為小數點和數字組成
#define NUMBERS @"0123456789."/** 判斷字串是否為數字和小數點 */-(BOOL)isOnlyhasNumberAndpointWithString:(NSString *)string{ NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet]; NSString *filter=[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return [string isEqualToString:filter];}
7.在tableview reloadSections時,即使把動畫設定為UITableViewRowAnimationNone時也會出現奇怪的動畫。如交錯等
[self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
此時有兩種解決方案
①將reloadSections替換為reloadData,重新整理局部改為重新整理整體,但這樣違背了初衷
②將動畫強製取消
[UIView performWithoutAnimation:^{ [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];}];
iOS工作筆記(十七)