开发细节(一)
1. iOS6+的UIScrollerView不滚动的问题
- setContentSize方法需要写到viewDidAppear里,而不是viewDidLoad里了。
- scrollEnabled需要设置为YES
- contentSize必须比控件的frame尺寸大
- 当做一些控件滑动效果时,最好是将pagingEnabled设置为NO,autoresizesSubviews最好也设置为NO。
2. 弧度与角度的转换
1角度 = π/180 弧度
1弧度 = 180/π角度
360角度 = 360 * π/180 = 2π弧度 = 一整圈
3. 图片的缩放操作
UIImage *bigImage = [UIImage imageNamed:@"myPhoto"]; CGSize scaleSize = CGSizeMake(80, 80); //Creates a bitmap-based graphics context and makes it the current context. (创建一个基于位图的context,并设置成当前正使用的context) UIGraphicsBeginImageContext(scaleSize); //Draws the entire image in the specified rectangle, scaling it as needed to fit. (在指定的区域內绘制整个图形,并缩放以适应该区域) [bigImage drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)]; //Returns an image based on the contents of the current bitmap-based graphics context. (从当前基于位图的context里得到图片) UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); //Removes the current bitmap-based graphics context from the top of the stack. (将当前基于位图的context从堆栈移除) UIGraphicsEndImageContext(); UIImageView *imageView = [[UIImageView alloc] initWithImage:smallImage]; [self.view addSubview:imageView];
4. 画线
CGSize scaleSize = CGSizeMake(200, 200); UIGraphicsBeginImageContext(scaleSize); CGContextRef cgContext = UIGraphicsGetCurrentContext(); //style CGContextSetLineCap(cgContext, kCGLineCapRound); CGContextSetLineWidth(cgContext, 3.0); CGContextSetRGBStrokeColor(cgContext, 1.0, 0.0, 1.0, 1.0); //line 1 CGContextBeginPath(cgContext); CGContextMoveToPoint(cgContext, 20, 20); CGContextAddLineToPoint(cgContext, 100, 100); CGContextStrokePath(cgContext); //line 2 CGPoint aPoints[3]; aPoints[0] =CGPointMake(100, 80); aPoints[1] =CGPointMake(130, 80); aPoints[2] =CGPointMake(60, 140); CGContextAddLines(cgContext, aPoints, 3); CGContextDrawPath(cgContext, kCGPathStroke); CGContextFlush(cgContext); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.view addSubview:imageView];
5. 随机数的生成
1. 获取一个随机整数范围:0<=rand<100 arc4random() % 100; 2. 获取一个随机整数范围:100<=rand<500 (arc4random() % 101) + 400;
3. 获取一个随机整数,from<=rand<to - (int)getRandomNumber:(int)from to:(int)to { return (int)(from + (arc4random() % (to – from + 1))); //+1,result is [from to]; else is [from, to) //from 100 to 500 //100 + arc4randow()% (500-100+1) //100 + arc4random()%401 }
6. 控件的复制
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: myLabel1];
UILabel *myLabel2 = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程