1:如何給表格單元列增加選擇時的背影效果
if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; cell.backgroundColor = [UIColor clearColor]; cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18]; cell.textLabel.textColor = [UIColor whiteColor]; cell.textLabel.highlightedTextColor = [UIColor lightGrayColor]; UIView *sbg=[[UIView alloc] initWithFrame:cell.frame]; sbg.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.3]; cell.selectedBackgroundView = sbg; }
2:修改標題欄的文字
UIColor *cc = [UIColor whiteColor]; NSDictionary * dict = [NSDictionary dictionaryWithObject:cc forKey:UITextAttributeTextColor]; self.navigationController.navigationBar.titleTextAttributes = dict;
3:一個滾動啟動頁功能代碼
#define NewFeatureCount 4@interface HVWNewFeatureViewController () <UIScrollViewDelegate>@PRoperty(nonatomic, strong) UipageControl *pageControl;@end@implementation HVWNewFeatureViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 添加scrollView [self setupScrollView]; // 添加pageControl [self setupPageControl];}/** 添加scrollView */- (void) setupScrollView { // 創建一個scrollView UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = self.view.bounds; // 添加圖片 for (int i=0; i<NewFeatureCount; i++) { // 獲取圖片 NSString *featureImageName = [NSString stringWithFormat:@"new_feature_%d", i+1]; UIImageView *featureImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithNamed:featureImageName]]; // 設置圖片尺寸位置 CGFloat featureWidth = self.view.width; CGFloat featureHeight = self.view.height; CGFloat featureX = featureImageView.width * i; CGFloat featureY = 0; featureImageView.frame = CGRectMake(featureX, featureY, featureWidth, featureHeight); // 如果是最后一頁,加上功能按鈕 if (i == (NewFeatureCount - 1)) { // 為了讓最后一頁的的功能按鈕能夠生效,必須激活交互功能 featureImageView.userInteractionEnabled = YES; [self addFunctionButton:featureImageView]; } // 添加圖片到scrollView [scrollView addSubview:featureImageView]; } // 設置scrollView功能屬性 scrollView.userInteractionEnabled = YES; scrollView.scrollEnabled = YES; // 支持滾動 scrollView.contentSize = CGSizeMake(self.view.width * NewFeatureCount, 0); // 只需要水平滾動 scrollView.pagingEnabled = YES; // 支持分頁 scrollView.showsHorizontalScrollIndicator = NO; // 隱藏水平滾動條 // 設置背景色 scrollView.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1.0]; // 設置代理 scrollView.delegate = self; // 添加 [self.view addSubview:scrollView];}/** 添加pageControl */- (void) setupPageControl { // pageControl不能加在scrollView上,不然會隨著內容一起滾動 UIPageControl *pageControl = [[UIPageControl alloc] init]; pageControl.pageIndicatorTintColor = [UIColor blackColor]; pageControl.currentPageIndicatorTintColor = [UIColor redColor]; pageControl.numberOfPages = NewFeatureCount; // 設置位置 pageControl.centerX = self.view.width * 0.5; pageControl.centerY = self.view.height * 0.9; self.pageControl = pageControl; [self.view addSubview:pageControl];}#pragma mark - UIScrollViewDelegate/** scrollView滾動代理方法,在這里控制頁碼指示器 */- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 四舍五入,讓圖片滾動超過中線的時候改變頁碼 self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.width + 0.5;}#pragma mark - 最后一頁的功能/** 添加功能按鈕 */- (void) addFunctionButton:(UIImageView *) imageView { // 添加"分享"選項按鈕 [self addShareButton:imageView]; // 添加"進入微博"按鈕 [self addEnterWeiboButton:imageView];}/** 分享選項按鈕 */- (void) addShareButton:(UIImageView *) imageView { // 創建按鈕 UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom]; [shareButton setTitle:@"分享給大家" forState:UIControlStateNormal]; [shareButton setImage:[UIImage imageWithNamed:@"new_feature_share_false"] forState:UIControlStateNormal]; [shareButton setImage:[UIImage imageWithNamed:@"new_feature_share_true"] forState:UIControlStateSelected]; [shareButton addTarget:self action:@selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // 位置尺寸 shareButton.size = CGSizeMake(150, 50); // 必須先設置了size,center才真的在中心,不然就是從左上角開始!!! shareButton.centerX = self.view.width * 0.5; shareButton.centerY = self.view.height * 0.65; // 設置內間距 shareButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10.0, 0, 0); // 添加 [imageView addSubview:shareButton];}/** 分享選項點擊事件方法 */- (void) shareButtonClicked:(UIButton *) button { button.selected = !button.selected;}/** “進入微博"按鈕 */- (void) addEnterWeiboButton:(UIImageView *) imageView { // 創建按鈕 UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom]; enterButton.userInteractionEnabled = YES; [enterButton setBackgroundImage:[UIImage imageWithNamed:@"new_feature_finish_button"] forState:UIControlStateNormal]; [enterButton setBackgroundImage:[UIImage imageWithNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted]; [enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [enterButton setTitle:@"進入微博" forState:UIControlStateNormal]; // 位置尺寸 enterButton.size = enterButton.currentBackgroundImage.size; enterButton.centerX = self.view.width * 0.5; enterButton.centerY = self.view.height * 0.8; // 監聽點擊 [enterButton addTarget:self action:@selector(enterWeiboButtonClicked) forControlEvents:UIControlEventTouchUpInside]; // 添加 [imageView addSubview:enterButton];}/** “進入微博” 按鈕點擊 */- (void) enterWeiboButtonClicked { UIWindow *window = [UIapplication sharedApplication].keyWindow; window.rootViewController = [[HVWTabBarViewController alloc] init];}@end
4:增加刪除控制器
增加控制器:[self addChildViewController:toViewController];[toViewController didMoveToParentViewController:self];小實例:- (IBAction)btnAction:(id)sender { CiderViewController *cid=[[CiderViewController alloc] init]; [self addChildViewController:cid]; CGRect frame=self.myView.bounds; frame.origin.y=110; frame.size.width=290; frame.size.height=90; cid.view.frame=frame; cid.view.backgroundColor=[UIColor redColor]; [self.myView addSubview:cid.view]; [cid didMoveToParentViewController:self];}刪除控制器:1.當我們向我們的視圖控制器容器中調用removeFromParentViewController方法時,必須要先調用該方法,且parent參數為nil:[將要刪除的視圖控制器 willMoveToParentViewController:nil];[fromViewController willMoveToParentViewController:nil];[fromViewController removeFromParentViewController];Í一些說明:關于willMoveToParentViewController方法和didMoveToParentViewController方法的使用1.這兩個方法用在子試圖控制器交換的時候調用!即調用transitionFromViewController 方法時,調用。2.當調用willMoveToParentViewController方法或didMoveToParentViewController方法時,要注意他們的參數使用:當某個子視圖控制器將從父視圖控制器中刪除時,parent參數為nil。即:[將被刪除的子試圖控制器 willMoveToParentViewController:nil];當某個子試圖控制器將加入到父視圖控制器時,parent參數為父視圖控制器。即:[將被加入的子視圖控制器 didMoveToParentViewController:父視圖控制器];3.無需調用[子視圖控制器 willMoveToParentViewController:父視圖控制器]方法。因為我們調用[父視圖控制器 addChildViewController:子視圖控制器]時,已經默認調用了。只需要在transitionFromViewController方法后,調用[子視圖控制器didMoveToParentViewController:父視圖控制器];4.無需調用[子視圖控制器 didMoveToParentViewController:父視圖控制器]方法。因為我們調用[子視圖控制器 removeFromParentViewController]時,已經默認調用了。只需要在transitionFromViewController方法之前調用:[子視圖控制器 willMoveToParentViewController:nil]。不錯的文章(http://www.cocoanetics.com/2012/04/containing-viewcontrollers/ http://mobile.51cto.com/iphone-313146.htm)
5:關于UIView的autoresizingMask屬性的研究
在 UIView 中有一個autoresizingMask的屬性,它對應的是一個枚舉的值(如下),屬性的意思就是自動調整子控件與父控件中間的位置,寬高。enum { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1 << 0, UIViewAutoresizingFlexibleWidth = 1 << 1, UIViewAutoresizingFlexibleRightMargin = 1 << 2, UIViewAutoresizingFlexibleTopMargin = 1 << 3, UIViewAutoresizingFlexibleHeight = 1 << 4, UIViewAutoresizingFlexibleBottomMargin = 1 << 5};UIViewAutoresizingNone就是不自動調整。UIViewAutoresizingFlexibleLeftMargin 自動調整與superView左邊的距離,保證與superView右邊的距離不變。UIViewAutoresizingFlexibleRightMargin 自動調整與superView的右邊距離,保證與superView左邊的距離不變。UIViewAutoresizingFlexibleTopMargin 自動調整與superView頂部的距離,保證與superView底部的距離不變。UIViewAutoresizingFlexibleBottomMargin 自動調整與superView底部的距離,也就是說,與superView頂部的距離不變。UIViewAutoresizingFlexibleWidth 自動調整自己的寬度,保證與superView左邊和右邊的距離不變。UIViewAutoresizingFlexibleHeight 自動調整自己的高度,保證與superView頂部和底部的距離不變。UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin 自動調整與superView左邊的距離,保證與左邊的距離和右邊的距離和原來距左邊和右邊的距離的比例不變。比如原來距離為20,30,調整后的距離應為68,102,即68/20=102/30。其它的組合類似。實例:CGRect frame = [[UIScreen mainScreen] applicationFrame];UIView *view = [[UIView alloc] initWithFrame:frame];view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
6:系統提供的dispatch方法
為了方便地使用GCD,蘋果提供了一些方法方便我們將block放在主線程 或 后臺線程執行,或者延后執行。使用的例子如下: 1 // 后臺執行: 2 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 3 // something 4 }); 5 // 主線程執行: 6 dispatch_async(dispatch_get_main_queue(), ^{ 7 // something 8 }); 9 // 一次性執行: 10 static dispatch_once_t onceToken; 11 dispatch_once(&onceToken, ^{ 12 // code to be executed once 13 }); 14 // 延遲2秒執行: 15 double delayInSeconds = 2.0; 16 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 17 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 18 // code to be executed on the main queue after delay 19 }); dispatch_queue_t 也可以自己定義,如要要自定義queue,可以用dispatch_queue_create方法,示例如下: 1 dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL); 2 dispatch_async(urls_queue, ^{ 3 // your code 4 }); 5 dispatch_release(urls_queue); 另外,GCD還有一些高級用法,例如讓后臺2個線程并行執行,然后等2個線程都結束后,再匯總執行結果。這個可以用dispatch_group, dispatch_group_async 和 dispatch_group_notify來實現,示例如下: 1 dispatch_group_t group = dispatch_group_create(); 2 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ 3 // 并行執行的線程一 4 }); 5 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ 6 // 并行執行的線程二 7 }); 8 dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{ 9 // 匯總結果 10 });
新聞熱點
疑難解答