1 点击背景时关闭键盘
在viewDidLoad中添加手势识别器
// add tap gesture recognizer UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)]; tapRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapRecognizer];
定义函数
- (void) handleBackgroundTap:(UITapGestureRecognizer*)sender { [phoneField resignFirstResponder]; [nameField resignFirstResponder]; [postcodeField resignFirstResponder]; }
在该函数使文本字段取消第一响应者状态.
2 图片作为填充颜色
[UIColor colorWithPatternImage: [UIImage imageNamed:@"background"];
3 创建和绘制渐变
View Code
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); UIColor *startColor = [UIColor blueColor]; CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]); UIColor *endColor = [UIColor greenColor]; CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]); CGFloat colorComponents[8] = { startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3], endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3], }; CGFloat colorIndices[2] = { 0.0f, 1.0f, }; CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, (const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2); CGColorSpaceRelease(colorSpace); CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGPoint startPoint, endPoint; CGContextRef context = UIGraphicsGetCurrentContext(); startPoint = CGPointMake(0.0f,screenBounds.size.height/2.0f); endPoint = CGPointMake(screenBounds.size.width, startPoint.y); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); CGGradientRelease(gradient);
4 加载显示网页
View Code
NSURL *url = [NSURL URLWithString:_detailItem]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [_webView loadRequest:request];
5 导航条加载图片做背景
6 UIView加阴影
View Code
sepLine = [[UIView alloc] init ]; sepLine.frame = CGRectMake(LEFT_MARGIN+5, 0, cellWidth-LEFT_MARGIN-RIGHT_MARGIN, 1); //加阴影 CAGradientLayer *bottomShadow = [[CAGradientLayer alloc] init]; bottomShadow.frame = CGRectMake(2, 0, cellWidth-LEFT_MARGIN-RIGHT_MARGIN+2, 1); CGColorRef black = [UIColor lightGrayColor].CGColor; CGColorRef white = [UIColor whiteColor].CGColor; bottomShadow.colors = [NSArray arrayWithObjects:(__bridge id)black,(__bridge id)white, nil]; [sepLine.layer addSublayer:bottomShadow];
7 iOS 同步 synchronization
使用NSLock和@synchronized来保护临界区
还可以用dispatch_sync
8 获取屏幕信息
UIScreen用于获取当前所有屏幕(包括设备主屏幕)的屏幕对象。屏幕对象包含屏幕的属性信息(包括将屏幕的尺寸和
像素比例也考虑在内的屏幕特征)。
9 资产库框架
iOS 4.0引入了资产库框架(AssetsLibrary.framework),该框架提供一个查询界面,您可以通过它查找用户照片和数据。通过使用该框架,您可以访问Photos管理的资产,包括用户保存的相册以及导入到设备中的图片或视频。而且您也可以将照片或者视频保存到用户的相册。