斯坦福Developing iOS 8 Apps学习笔记(一)
来源:互联网 发布:spark排序算法 编辑:程序博客网 时间:2024/05/17 22:41
Logistics, iOS 8 Overview, Swift, Foundation
第一、二、三、四节课感觉是比较杂乱的一些Cocoa和Swift知识
Printable协议
遵守Printable协议可以在print(“(someElement)”)时正确地输出
enum的使用
enum在Swift中是一个非常好用的东西,对于一个值可能有几种枚举值的情况一定要使用enum,并且Swift的enum可以链接属性值,使用起来非常方便
NSNumber
- NSNumberFormatter
- NSNumber有doubleVlue,intValue等,在Swift中NSNumber用处不大,但要注意NSNumberFormatter能够方便地进行类型转化
var stringNumber = "123"var formatter = NSNumberFormatter()var intNumber = formatter.numberFromString(stringNumber)
NSDate
- NSCalendar
- NSDateFormatter
- NSDateComponent
let formatter = NSDateFormatter()if NSData().timeIntervalSinceData(tweet.created)>24*60*60 formatter.dataStyle = NSDataFormatterStyle.shortStyleelse formatter.timeStyle = NSDataFormatterStyle.shortStyletweetCreatedLabel?.text = formatter.stringFromDate(tweet.created)
NSData
NSData的本质是一个比特包
Swift Array
- Filter
- map
- reduce
var array = [1,2,3,4,5] var array1 = array.filter { return $0 > 3 } println(array1) var array2 = array.map { (initial) -> String in return "\(initial)" } println(array2) var sum = array.reduce(0, combine: { (sum, toAdd) -> Int in return sum + toAdd }) println(sum)
Swift String
Swift String不能简单地使用str[2]形式来访问其中的字符,因为Unicode字符可能由多位构成,使用String.index类型来作为索引,依靠advance函数来获取String.index索引
var str = "Hello"let index = advance(str.startIndex, 2)str.splice("abc", atIndex: index) //str = "Heabcllo"let startIndex = advance(str.startIndex, 2)let endIndex = advance(str.startIndex, 5)let subString = str[startIndex...endIndex] //subString = "abcl"var array3 = ["!","?",":"]var str1 = str.join(array3) //str1 = "!Heabcllo?Heabcllo:"str.componentsSeparatedByString("c") // ["Heab", "llo"]
一些有用的全局函数
- countElements(a collection)
- sub = dropFirst(a sliceable)
- sub = dropLast(a sliceable)
- first = first(a collection)
- last = last(a collection)
- prefix = prefix(a sliceable, x : Int)
- suffix = suffix(a sliceable, x : Int)
- reversed : Array = reverse(a collection)
- backwards : String = String(reverse(s))
绘图相关
CGRect类
- minX
- midY
- intersects(CGRect) -> Bool 是否相交
- intersect(CGRect) 两矩形相交的部分
- contains(CGPoint) -> Bool
- 这其中的单位是Point点(可以包含多个像素),可由contentScaleFactor属性获得
bounds和frame的关系
- bounds是自身的坐标系,用于在自身视图中定位
- center和frame相对于父系坐标系,与绘制无关,用于定位父视图中的此视图
在view中绘图
- 在view中绘图,使用
override func drawRect(regionThatNeedsToBeDrawn:CGRect)
- 永远不要自己调用drawRect。Instead, uses
setNeesDisplay()setNeedsDisplayInRect(regionThatNeedsToBeDrawn:CGRect)
绘图两种方式
- Core Graphics
① context上下文 UIGraphicsGetCurrentContext()
② Create Path(lines,arcs, etc.)
③ drawing attributes(colors,font,textures,linewidths,linecaps,…)
④ stroke or fill paths with attributes - UIBezierPath(面向对象封装,更好用一些)
① don’t worry arbout context
② lineto,arcs,… linewidth
③ fill or stroke
let path = UIBezierPath() path.moveToPoint(CGPoint(x: 80, y: 50)) path.addLineToPoint(CGPoint(x: 140, y: 150)) path.addLineToPoint(CGPoint(x: 20, y: 30)) path.closePath() UIColor.greenColor().setFill() path.lineWidth = 3.0 path.fill() let roundRect = UIBezierPath(roundedRect: CGRect(x: 50, y: 50, width: 50, height: 50), cornerRadius: 5) let oval = UIBezierPath(ovalInRect: CGRect(x: 50, y: 50, width: 100, height: 100)) roundRect.addClip() //可以用于用圆角矩形Clip纸牌图案 oval.containsPoint(CGPoint(x: 70, y: 70))
UIColor
- 支持RGB,HSB,pattern(UIImage)
let transparentYellow = UIColor.yellowColor().colorWithAlphaComponent(0.5)
- 透明绘制意味着opaque=false, hidden = false
文字绘制
- NSForegroundColorAttributeName:UIColor- NSStrokeWidthAttributeName:CGFloat- NSFontAttributeName:UIFont其中UIFont一般情况下我们可以使用以下方法- UIFont.preferredFontForTextStyle(UIFontTextStyle) ->UIFont- UIFontTextStyle{Headline,Body,Footnote...}- UIFont.systemFontOfSize(pointSize:CGFloat) -> UIFont- UIFont.bodysystemFontOfSize(pointSize:CGFloat) -> UIFont
- 文字绘制按照上面的代码作为模板即可,其中attributes有以下设置。
- NSForeGrounColorAttributeName:UIColor
- NSStrokeWidthAttributeName:CGFloat
- NSFontAttributeName:UIFont
- UIFont可以通过以下方法来得到
- UIFont.preferredFontForTextStyle(UIFontTextStyle) -> UIFont
- UIFontTextStyle{Headline, Body, Footnote}
- UIFont.systemFontOfSize(pointSize:CGFloat) -> UIFont
- UIFont.boldSystemFontOfSize(pointSize:CGFloat) -> UIFont
- 其中system的字体一般用于segmented Controller等这些系统控件中的文字,User Content一般是preferredFont。
图像绘制
image = UIImage(named:"foo") UIImage(contentsOfFile:String) UIImage(data:NSData)image.drawAtPoint(CGPoint)iamge.drawInRect(CGRect)image.drawAsPatternInRect(CGRect) //平铺
需要注意的
- 当UIView的bounds改变的时候(例如旋转设备,AutoLayout使得bounds改变),以上绘制是没有redraw的,需要改变UIView的contentMode(storyboard或是代码中都可以改)
- UIViewContentMode
- .Left/.Right/…/.BottomLeft/.Center
- .ScaleToFill(Default)/.ScaleAspectFill(保持长宽比例填满imageView,可能有部分图片显示不出来)/.ScaleAspectFit(保持长宽比例尽可能大的撑imageView,imageView可能有空白)
- 一个很好用的方法:convertPoint(CGPoint, fromView:UIView)
感谢斯坦福大学的公开课程,白胡子老爷爷讲得真的很棒,为知识的分享点赞!课程链接,仅用于知识分享。如有错误或是遗漏,敬请指正!
0 0
- 斯坦福Developing iOS 8 Apps学习笔记(一)
- 斯坦福Developing iOS 8 Apps学习笔记(二)
- 斯坦福Developing iOS 8 Apps学习笔记(三)
- 斯坦福Developing iOS 8 Apps学习笔记(四)
- 斯坦福Developing iOS 8 Apps学习笔记(五)
- 斯坦福Developing iOS 8 Apps学习笔记(六)
- 斯坦福Developing iOS 8 Apps学习笔记(七)
- 《Start Developing iOS Apps Today》学习笔记(一)部分翻译
- Developing iOS 8 Apps with Swift 学习笔记
- 《Start Developing iOS Apps Today》学习笔记(三)心得体会
- Start Developing iOS Apps (Swift) 学习笔记 (1)
- Start Developing iOS Apps (Swift) 学习笔记 (2)
- 《Start Developing iOS Apps Today》学习笔记(二)应用实现过程
- Developing iOS 8 Apps with Swift Lesson 11学习
- 学习stanford公开课 Developing iOS8 Apps with Swift 笔记(一)
- Start Developing iOS Apps Today
- Start Developing iOS Apps Today
- Start Developing iOS Apps Today
- 使用GDB调试core文件时的一些基本知识
- 开发管理中常用的命令
- STL 配置器和容器,序列容器和关联容器总结
- MySQL cmake安装
- Spring4+Hibernate4整合经验:事务配置导致的java.lang.NoSuchMethodError异常
- 斯坦福Developing iOS 8 Apps学习笔记(一)
- 关于JavaScript中function的两种创建方式的解析
- linux进程调度原理
- Hadoop Yarn 框架原理及运作机制
- C3P0连接池配置信息记录备查
- 投资火 防互联网医疗透支
- 进程通信-dbus(1)
- fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
- 减资程序&案例