IOS开发基础知识--碎片10
1:如何给表格单元列增加选择时的背影效果
if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:];
        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=; i<NewFeatureCount; i++) {
        // 获取图片
        NSString *featureImageName = [NSString stringWithFormat:@"new_feature_%d", i+];
        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 = ;
        featureImageView.frame = CGRectMake(featureX, featureY, featureWidth, featureHeight);
        // 如果是最后一页,加上功能按钮
        if (i == (NewFeatureCount - )) {
            // 为了让最后一页的的功能按钮能够生效,必须激活交互功能
            featureImageView.userInteractionEnabled = YES;
            [self addFunctionButton:featureImageView];
        }
        // 添加图片到scrollView
        [scrollView addSubview:featureImageView];
    }
    // 设置scrollView功能属性
    scrollView.userInteractionEnabled = YES;
    scrollView.scrollEnabled = YES; // 支持滚动
    scrollView.contentSize = CGSizeMake(self.view.width * NewFeatureCount, ); // 只需要水平滚动
    scrollView.pagingEnabled = YES; // 支持分页
    scrollView.showsHorizontalScrollIndicator = NO; // 隐藏水平滚动条
    // 设置背景色
    scrollView.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/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(, );
    // 必须先设置了size,center才真的在中心,不然就是从左上角开始!!!
    shareButton.centerX = self.view.width * 0.5;
    shareButton.centerY = self.view.height * 0.65;
    // 设置内间距
    shareButton.titleEdgeInsets = UIEdgeInsetsMake(, 10.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=;
frame.size.width=;
frame.size.height=; cid.view.frame=frame;
cid.view.backgroundColor=[UIColor redColor];
[self.myView addSubview:cid.view];
[cid didMoveToParentViewController:self];
} 删除控制器: .当我们向我们的视图控制器容器中调用removeFromParentViewController方法时,必须要先调用该方法,且parent参数为nil:
[将要删除的视图控制器 willMoveToParentViewController:nil]; [fromViewController willMoveToParentViewController:nil];
[fromViewController removeFromParentViewController];Í 一些说明: 关于willMoveToParentViewController方法和didMoveToParentViewController方法的使用 .这两个方法用在子试图控制器交换的时候调用!即调用transitionFromViewController 方法时,调用。 .当调用willMoveToParentViewController方法或didMoveToParentViewController方法时,要注意他们的参数使用:
当某个子视图控制器将从父视图控制器中删除时,parent参数为nil。
即:[将被删除的子试图控制器 willMoveToParentViewController:nil];
当某个子试图控制器将加入到父视图控制器时,parent参数为父视图控制器。
即:[将被加入的子视图控制器 didMoveToParentViewController:父视图控制器]; .无需调用[子视图控制器 willMoveToParentViewController:父视图控制器]方法。因为我们调用[父视图控制器 addChildViewController:子视图控制器]时,已经默认调用了。
只需要在transitionFromViewController方法后,调用[子视图控制器didMoveToParentViewController:父视图控制器]; .无需调用[子视图控制器 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                 = ,
   UIViewAutoresizingFlexibleLeftMargin   =  << ,
   UIViewAutoresizingFlexibleWidth        =  << ,
   UIViewAutoresizingFlexibleRightMargin  =  << ,
   UIViewAutoresizingFlexibleTopMargin    =  << ,
   UIViewAutoresizingFlexibleHeight       =  << ,
   UIViewAutoresizingFlexibleBottomMargin =  <<
};
UIViewAutoresizingNone就是不自动调整。
UIViewAutoresizingFlexibleLeftMargin 自动调整与superView左边的距离,保证与superView右边的距离不变。
UIViewAutoresizingFlexibleRightMargin 自动调整与superView的右边距离,保证与superView左边的距离不变。
UIViewAutoresizingFlexibleTopMargin 自动调整与superView顶部的距离,保证与superView底部的距离不变。
UIViewAutoresizingFlexibleBottomMargin 自动调整与superView底部的距离,也就是说,与superView顶部的距离不变。
UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与superView左边和右边的距离不变。
UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与superView顶部和底部的距离不变。
UIViewAutoresizingFlexibleLeftMargin  |UIViewAutoresizingFlexibleRightMargin 自动调整与superView左边的距离,保证与左边的距离和右边的距离和原来距左边和右边的距离的比例不变。比如原来距离为20,,调整后的距离应为68,,即68/=/。
其它的组合类似。
实例:
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:frame];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
6:系统提供的dispatch方法
为了方便地使用GCD,苹果提供了一些方法方便我们将block放在主线程 或 后台线程执行,或者延后执行。使用的例子如下:
        //  后台执行: 
         dispatch_async(dispatch_get_global_queue(, ), ^{ 
              // something 
         }); 
         // 主线程执行: 
         dispatch_async(dispatch_get_main_queue(), ^{ 
              // something 
         }); 
         // 一次性执行: 
         static dispatch_once_t onceToken; 
         dispatch_once(&onceToken, ^{ 
             // code to be executed once 
         }); 
         // 延迟2秒执行: 
         double delayInSeconds = 2.0; 
         dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
         dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
             // code to be executed on the main queue after delay 
         }); 
dispatch_queue_t 也可以自己定义,如要要自定义queue,可以用dispatch_queue_create方法,示例如下:
        dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL); 
        dispatch_async(urls_queue, ^{ 
             // your code 
        }); 
        dispatch_release(urls_queue); 
另外,GCD还有一些高级用法,例如让后台2个线程并行执行,然后等2个线程都结束后,再汇总执行结果。这个可以用dispatch_group, dispatch_group_async 和 dispatch_group_notify来实现,示例如下:
        dispatch_group_t group = dispatch_group_create(); 
        dispatch_group_async(group, dispatch_get_global_queue(,), ^{ 
              // 并行执行的线程一 
         }); 
         dispatch_group_async(group, dispatch_get_global_queue(,), ^{ 
              // 并行执行的线程二 
         }); 
         dispatch_group_notify(group, dispatch_get_global_queue(,), ^{ 
              // 汇总结果 
         }); 
IOS开发基础知识--碎片10的更多相关文章
- IOS开发基础知识碎片-导航
		
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
 - IOS开发基础知识--碎片3
		
十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...
 - IOS开发基础知识--碎片19
		
1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...
 - IOS开发基础知识--碎片33
		
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
 - IOS开发基础知识--碎片42
		
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
 - IOS开发基础知识--碎片47
		
1:解决ios静态库中的类别(category)在工程中不能使用 解决方法为:找到 target 的图标,更改其 Other Linker Flags 为: -all_load 或 -force_lo ...
 - IOS开发基础知识--碎片50
		
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
 - IOS开发基础知识--碎片11
		
1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...
 - IOS开发基础知识--碎片14
		
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
 
随机推荐
- herbnate session.createSQLQuery(sql) 和 session.createQuery(sql)使用
			
public class DistributeDao implements Serializable{ private SessionFactory sessionFactory; public Se ...
 - 初学ReactJS,写了一个RadioButtonList组件
			
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>React Demo</title> ...
 - T-SQL:毕业生出门需知系列(八)
			
第8课 使用函数处理数据 8.1 函数 [名词]可移植:所编写的代码可以在多个系统上运行 8.2 使用函数 8.2.1 文本处理函数 例1:使用 UPPER() 函数--将文本转换为大写 SELECT ...
 - 十进制(decimal system)转换函数说明
			
一,十进制(decimal system)转换函数说明 1,十进制转二进制 decbin() 函数,如下实例 echo decbin(12); //输出 1100 echo decbin(26); / ...
 - MySQL分区总结
			
MySQL支持RANGE,LIST,HASH和KEY四种分区.其中,每个分区又都有一种特殊的类型.对于RANGE分区,有RANGE COLUMNS分区.对于LIST分区,有LIST COLUMNS分区 ...
 - 如何访问facebook (转)
			
对于普通大众,访问facebook需要两个条件:1)使用代理 2)翻译网页内容.本文将介绍怎样安全高速地访问诸如facebook之类的国外网站,并提供相关软件下载. 工具/原料 chromeGAE软件 ...
 - Android PopupWindow怎么合理控制弹出位置(showAtLocation)
			
说到PopupWindow,应该都会有种熟悉的感觉,使用起来也很简单 // 一个自定义的布局,作为显示的内容 Context context = null; // 真实环境中要赋值 int layou ...
 - mysql小数格式化正确方法
			
用到小数格式化,mysql了解很肤浅,只会简单的sql语句,于是百度,发现大家都是转载同一个文章,好无语. 而且,结果验证还是不正确,查了官方api,终于写出来了. 另外,还是保存下百度的几个方法: ...
 - .Net语言 APP开发平台——Smobiler学习日志:如何在手机上开发仪表盘控件
			
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的"S ...
 - ActiveX(五)更好的“ActiveX”?
			
前文中四篇随笔.已经可以实现 ActiveX 与 Js 无缝交互. 也就是说借用ActiveX实现更加强大的功能已经完全不是问题.但是.ActiveX 本身还有一个局限性——浏览器兼容问题.如此强大的 ...